I need to update all the objects in the database, when I try it, it says:
Thread 1: "Attempting to modify a frozen object - call thaw on the Object instance first."
I have the following:
@ObservedRealmObject var meal: MealTracking
@ObservedResults(MealTracking.self) var mealTracking
mealTracking is the one that contains everything and meal is the single object in the current view.
So once I update the name on that single object (meal), I want to update the name on all other objects. So I'm doing:
for (index, meal) in self.mealTracking.enumerated() {
$mealTracking.wrappedValue[index].mealName = ""
}
and I get the error of not being able to update the objects.
I also tried it like $mealTracking[index].mealName.wrappedValue = ""
and gives another error: Referencing subscript 'subscript(_:)' requires wrapped value of type 'Results<MealTracking>'
For a the single object on the current view I can update it with:
$meal.mealName.wrappedValue = "some stuff"
, the problem is when attempting to update All objects
How can I update all the objects?
CodePudding user response:
@ObservedRealmObject
is a frozen object. If you want to modify the properties of an @ObservedRealmObject directly in a write transaction, you must .thaw()
it first.
In your case something like :
mealName.thaw()?.name = ""
More information :
https://www.mongodb.com/docs/realm/sdk/swift/swiftui/
https://www.mongodb.com/community/forums/t/freeze-frozen-objects-and-thawing/15706/5