Error : ***** Terminating app due to uncaught exception 'RLMException', reason: 'Cannot modify managed RLMArray outside of a write transaction.'**
I followed this documentation, which works. I ran into an error when I tried adding two relationships. See code below:
class Dog: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var name = ""
@Persisted var owner: Person? // I beleive this is causing the error
convenience init(name: String = "", owner: Person) {
self.init()
self.owner = owner
}
}
class Person: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var name = ""
@Persisted var dogs: List<Dog>
convenience init(name: String) {
self.init()
self.name = name
}
}
struct AddDogToPersonView: View {
@ObservedRealmObject var person: Person
@Environment(\.dismiss) var dismiss
var body: some View {
Form {
Section {
Button(action: {
$person.dogs.append(Dog(name: "Benny", owner: person))
dismiss.callAsFunction()
}) {
Text("Save")
}
Button(action: {
dismiss.callAsFunction()
}) {
Text("Cancel")
}
}
}
}
}
Could someone please explain what’s going on? The error says I can’t manage outside of a write transaction but it’s visible inside. Perhaps it has something to do with the fact that the dog
has a reference to its owner. Here:
$person.dogs.append(Dog(name: "Benny", owner: person))