在我的應用程序中,我有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