开发者

How to discard references to any managed objects fetched using the receiver?

The documentation for the -reset method of NSManagedObjectContext is confusing...

All the receiver's managed objects are “forgotten.” If you use this method, you should ensure that 开发者_JS百科you also discard references to any managed objects fetched using the receiver, since they will be invalid afterwards.

So what does that mean? When I have fetched any objects from the persistent store, and then reset my context, how would I "discard references"? Why must I do that?


If you get a NSManagedObject from the store somewhere in your code, then somewhere else you reset the the context, now you change the ManagedObject and try to persist it to the store through the context (which is reset and has no references to any objects) it will crash your app.

The context is a "scratch pad", it has "access" to all the stuff in the store, but it only deals with/has drawn in, Objects that you retrieved through it after the app launched. When you read in an NSManagedObject, the context (as far as I know) makes a copy and track all changes you make to it, it does nothing to the store before you persist it. This is what makes for the memory management in core data and the easy implementation of undo-redo etc.

So resetting the context also throws away these changes and there is nothing to persist to the store.

EDIT://added

The reference you should no pay to much attention to. You don't have to release it or the like, Core Data will take care of that, just don't reference the ManagedObject and the autorelease pool will discard of it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜