SwiftData doesn't parse my object correctly

Hello,

im trying to parse a JSON Request into a Swift Data Model. The specific bug happens with the nutriscore.

It works perfectly fine if I do my request and Decode or Encode the Model directly. But when im trying to add it to the Database the field is empty.

Somehow when im iterating through each product (my model) and console log it, it works. But only if there is one product in my database. sometimes when im adding another product, it somehow deletes(?) all nutriscores and the console log prints "nil" for all products. even for the product it worked before.

This right here is the way I insert into my database which should be perfectly fine. And the printing works also perfectly fine and it always displays the correct nutriscore

func dataScanner(_ dataScanner: DataScannerViewController, didAdd addedItems: [RecognizedItem], allItems: [RecognizedItem]) {
            for item in addedItems {
                if case let .barcode(barcode) = item {
                    performAPIRequest(with: barcode.payloadStringValue!) { result in
                        switch result {
                        case .success(let product):
                            ...
                            var descriptor: FetchDescriptor<Product> {
                                var descriptor = FetchDescriptor<Product>(
                                    predicate: #Predicate { $0.code == product.code }
                                )
                                
                                descriptor.fetchLimit = 1
                                
                                return descriptor
                            }
                            var products: [Product] = []
                            
                            products = try! self.parent.context.fetch(descriptor)
                            
                            print(product.nutriscore ?? "no nutri"); //WORKS PERFECTLY FINE!!
                            
                            if let existingProduct = products.first {
                                existingProduct.amount! += 1
                                existingProduct.lastScannedAt = Date()
                                
                            } else {
                                self.parent.context.insert(product)
                            }
                            self.processMeals()
                            case .failure(let error):
                            print("Error: \(error)")
                        }
                    }
                }
            }
        }

This has to be a SwiftData Bug, or why doesn't it work

I have correct my bug. It seems like that there is a temporary storage. When I scan my products the nutriscore seems to be stored only in that session. When I close the app and open it again, the nutriscore is not shown. I just believe think that there is an error with deeply nested models maybe? this is the important part of the json. maybe wait for wwdc 2024?

"nutriscore" : {
    "2023" : {
      "score" : 0,
      "category_available" : 1,
      "grade" : "a",
      "data" : {
        "positive_points_max" : "18.0",
        "is_cheese" : "0.0",
        "components" : {
          "negative" : [
            {
              "points_max" : 10,
              "unit" : "kJ",
              "points" : 0,
              "value" : 0,
              "id" : "energy"
            },
            {
              "unit" : "g",
              "points" : 0,
              "value" : 0,
              "points_max" : 10,
              "id" : "sugars"
            },
            {
              "unit" : "g",
              "points" : 0,
              "id" : "saturated_fat",
              "value" : 0,
              "points_max" : 10
            },
            {
              "points" : 0,
              "unit" : "g",
              "value" : 0,
              "id" : "salt",
              "points_max" : 20
            },
            {
              "points" : 0,
              "value" : 0,
              "id" : "non_nutritive_sweeteners"
            }
          ],
          "positive" : [
            {
              "id" : "proteins",
              "points" : 0,
              "unit" : "g",
              "value" : 0,
              "points_max" : 7
            },
            {
              "unit" : "g",
              "value" : 0,
              "id" : "fiber",
              "points" : 0,
              "points_max" : 5
            },
            {
              "points_max" : 6,
              "value" : 0,
              "unit" : "%",
              "id" : "fruits_vegetables_legumes",
              "points" : 0
            }
          ]
        },
        "is_beverage" : "1.0",
        "count_proteins" : "1.0",
        "negative_points" : "0.0",
        "negative_points_max" : "50.0",
        "count_proteins_reason" : "beverage",
        "is_fat_oil_nuts_seeds" : "0.0",
        "is_red_meat_product" : "0.0",
        "positive_nutrients" : [
          "proteins",
          "fiber",
          "fruits_vegetables_legumes"
        ]
      },
      "nutriscore_applicable" : 1,
      "nutriscore_computed" : 1,
      "nutrients_available" : 1
    }
  },
SwiftData doesn't parse my object correctly
 
 
Q