在我的应用程序中,我有UITableViewController
显示事件列表。此控制器使用ManagedObjectContext说ParentContext
。现在,如果选择了任何事件,则会显示详细的视图控制器,用户可以在其中编辑事件的详细信息。所以,我创建了一个孩子方面说,核心数据多级父 - 子环境
ChildContext with type "NSPrivateQueueConcurrencyType"
ChildContext whose parent Context is "ParentContext".
我的代码是:
NSManagedObjectContext *childContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
childContext.parentContext = self.context ;
现在再次有一些字段和关系这需要另外的向下钻取。所以我创造了另一种ChildContext新视图控制器说,
GrandChildContext with type "NSPrivateQueueConcurrencyType"
GrandChildContext whose parent context is "ChildContext"
这个过程也适用于其他级别(从父(的tableView共4级)儿童)
self.context - Parent Context
|
|
ChildContext
|
|
GrandChildContext
|
|
GrandGrandChildContext
我的实体看上去像这样
EntityA -- (Edit View Controller - uses ChildContext)
|
|- Field1
|
|- Field2
|
|- RelationShip (1 to Many) - (Relationship Add/Edit View Controller - uses GrandChildContext)
|
|- Field1
| .
| .
|- Field3
|
|- Relationship (1 to Many) - (Relationship Add/Edit View Controller - uses GrandGrandChildContext)
|
|- Field1
|
|- Field2
这是使用父 - 子上下文的正确方法吗?因为在某个时间点,我会像1 NSMainQueueConcurrencyType MOC and 3 NSPrivateQueueConcurrencyType MOC
一样。
如果不是?有没有其他方法?
是否有太多的子上下文影响应用程序的性能?
最初我使用属性和NSArrays来管理用户输入的数据,当用户点击完成按钮时,我将更新/创建托管对象。但这是一个乏味的工作,它让我的视图控制器很脏。所以我切换到父 - 子上下文,这很容易保存/丢弃更新。
由于
在所有的演习中,我都有编辑任务。我在一些钻取中设法减少了一些保存/取消操作。在使用此模型一段时间后,我发现的唯一问题是,创建的关系在父级子项中不可见。这只发生在ios 5中,这是一个已知的bug,我通过在保存子上下文之前调用obtainPermanentIDs来修复它。 – krishnan 2013-04-12 01:53:14
哦,我的天啊,+ krishnan,在我发现这个注释之前,我把我的头发撕了一半,并尝试添加一个acquirePermanentIDs调用。 – 2013-07-22 15:20:23