1

There two entities Parent and Child, which is one to many relationship. One Parent and many Child.

In a ChildView, I will get current Child level data's Parent by self.child.parent. But error occurs when update Parent in the following code and the error line has been pointed.

import SwiftUI
import CoreData

struct ChildView: View {

    var child: Child

    @ObservedObject private var db = CoreDataDB<Child>

    var body: some View {
        VStack {

            Button(action: { 
                CoreData.stack.context.delete(self.child)
                self.update()
            }) {
                Text("delete this child data")
            }
        }

    }

    func update() {

        let predicate: NSPredicate? = NSPredicate(format: "parent = %@", self.child.parent) // predicate by current child's parent 
        let children = self.db.loadDB(predicate: predicate)

        if children.count != 0 {
            children.first!.parent.updateCount(count: children.first!.parent.count) <-- Error occurs in this line when the button tapped every time
        }
    }

}

[error] error: Mutating a managed object 0x8575d55f78b4751a (0x6000009332f0) after it has been removed from its context. CoreData: error: Mutating a managed object 0x8575d55f78b4751a (0x6000009332f0) after it has been removed from its context.

[error] error: Mutating a managed object 0x8575d55f7888751a (0x60000096e940) after it has been removed from its context. CoreData: error: Mutating a managed object 0x8575d55f7888751a (0x60000096e940) after it has been removed from its context.

I do not find the impact by this error now, the APP is well and the CoreData is well. But I do not know whether it will impact my APP in the future. The second answer of this question is like mine.

How to fix this error? Thanks for any help.

Muz
  • 699
  • 1
  • 11
  • 25

0 Answers0