Core data Multi level parent - Child context

穿精又带淫゛_ 提交于 2019-12-04 07:09:47

You might have gone a little overboard with the multiple child contexts, but only a little, and your general approach is sound. A MOC (managed object context) is a fairly lightweight object.

I like your approach of having a distinct reference, within each view controller/scene, to the MOC that applies to that scene.

It is sometimes helpful to think about a MOC as a session or scratchpad. The matchup is not between MOCs and Entities, but rather between MOCs and logical units of work.

If one of your drill downs marks the beginning of some editing task that the user might want to abandon/cancel, that's a good moment to spin off a child MOC and pass it down to the new view. You can, if needed, rollback: or even just abandon the MOC, as you unwind back to the starting point.

On the other hand, if you were writing simply a viewer for static information, use just one MOC. In that case there is no need to or benefit from using more.

Perhaps there is a slight amount of confusion about the best use case for nested managed object contexts. For your case I would recommend to use only one single context.

Moving to Core Data from arrays etc. was a very good idea. Now unleash the true power and simplicity of the object graph. Try to keep things simple.

In order to drill down, just pass the context to the child view controller. Your child view controllers fetched results controller can use the same context as the parent view controller. Numerous Apple code examples use exactly this pattern.

The only time you need contexts is if you really need concurrency. This does not seem to be the case here at all. The new interface of the child view controller is shown once the data has been retrieved. If that takes too long (say, because the data comes from a web service) you show some kind of "please wait" interface and display the complete interface once the data retrieval is finished. Most likely this is not your scenario.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!