Is it safe to use a retained NSManagedObject after removal from managed object context?
i have an NSManagedObject that has just attributes(no fetched properties or relationshiops). I retain th开发者_如何学编程e instance, tell the managed context to remove the object from the graph, i persist the managed context.
is it safe to use the instance after the removal from the managed context?
Step by step:
->retain NSManagedObject
->remove NSManagedObject from managed object context
->persist changes to the managed object context
->use NSManagedObject
I don't believe so. A NSManagedObject belongs to the store. You use the store to create and destroy it. Be very cautious if you decide to do otherwise because as the NSManagedObject docs say, "it is important to remember that Core Data reserves exclusive control over the life cycle of the managed object (that is, raw memory management)."
->remove NSManagedObject from managed object context
Doesn't this imply that the object is released anyway? Certainly you can ignore it from then on without leaking.
Because you have a simple object with only attributes just copy them if you need to keep them around.
Keep in mind that if you are retained an object, you are responsible for releasing it. As for a deleted object: You will still have a non-zero retain count, but you must take measures not to use it, since it will lead to a "Core Data could not fulfil a fault" message (see this)
精彩评论