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

前端 未结 1 1719
野的像风
野的像风 2021-01-25 18:06

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

All the receiver\'s managed objects are “forgotten.” If you use this m

相关标签:
1条回答
  • 2021-01-25 18:42

    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 讨论(0)
提交回复
热议问题