开发者

Lifetime of Core Data managed objects

开发者 https://www.devze.com 2023-02-15 23:33 出处:网络
I observed the following behavior on my instances of NSManagedObject. ObjA is an instance of NSManagedObject loaded from NSManagedObjectContext ContextA.

I observed the following behavior on my instances of NSManagedObject.

  1. ObjA is an instance of NSManagedObject loaded from NSManagedObjectContext ContextA.
  2. I retain ObjA
  3. I access PropertyA of ObjA (ObjA has no relationships, only basic properties) - all works fine at this stage
  4. I开发者_Python百科 delete ObjA from ContextA
  5. I save ContextA
  6. ObjA is not deallocated (because of the extra retain I performed on it). I can still check it's retainCount for example.
  7. I access PropertyA of ObjA -> I get SIGABRT

I'm wondering whether this is the way CoreData was designed to work or a side effect from my code.


Just like this question you ask about holding onto NSManagedObjects after you have told the store they are deleted. The NSManagedObject docs say "t is important to remember that Core Data reserves exclusive control over the life cycle of the managed object (that is, raw memory management)." Copy the information you need, don't use a managed object after you have told the store it is released (deleted from the store), because it really is.

0

精彩评论

暂无评论...
验证码 换一张
取 消