在我的应用程序中我有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
.
如果不是?还有其他办法吗?
过多的子上下文是否会影响应用程序的性能?
最初我使用 Properties 和 NSArrays 来管理用户输入的数据,当用户点击完成按钮时,我将更新/创建托管对象。但这是一项乏味的工作,它使我的视图控制器变脏。所以我切换到父子上下文,这很容易保存/丢弃更新。
Thanks