2013-01-18 39 views
9

此問題可能看起來像之前已被問過很多次,但我不確定我是否正確彙總了答案。所以在這裏。核心數據對象ID與永久對象ID

ObjectID由Apple(WWDC 2012 Session 214)描述爲上下文安全的,線程安全的。所以我花了一些時間來轉換我的代碼以利用它。但是,它看起來並不像上下文那樣安全,因爲正如討論的hereCore Data: Do child contexts ever get permanent objectIDs for newly inserted objects?和其他地方那樣,存在稱爲永久標識的東西。

至於此永久ID的業務,我看了NSManagedObjectContext.h:

/* Converts the object IDs of the specified objects to permanent IDs. This implementation 
will convert the object ID of each managed object in the specified array to a permanent 
ID. Any object in the target array with a permanent ID will be ignored; additionally, 
any managed object in the array not already assigned to a store will be assigned, based on 
the same rules Core Data uses for assignment during a save operation (first writable store 
supporting the entity, and appropriate for the instance and its related items.) Although 
the object will have a permanent ID, it will still respond positively to -isInserted until 
it is saved. If an error is encountered obtaining an identifier, the return value will be 
NO. 
*/ 

- (BOOL)obtainPermanentIDsForObjects:(NSArray *)objects error:(NSError **)error NS_AVAILABLE(10_5, 3_0); 

所以我一直有在我的代碼與這個麻煩。我有NSManagedObjectContexts(比如說B和C)的層次結構,其中只有一個實際鏈接到一個持久存儲(調用是A)。所以,C是B的孩子,B是A的孩子。如果我從C創建一個NSManagedObject,然後調用obtainPermanentIDsForObjects,它實際上是永久的嗎?因爲.h文件的註釋讀起來就像它只查找層次結構B(第一個可寫存儲支持實體,並且在子母設置中,更改只推高了1級),而不是A.

感謝提前。

+0

當在子上下文中獲取對象的永久標識時,我收到「核心數據無法完成錯誤」。你有沒有發現,如果這是不允許的? –

回答

2

是的,如果您致電obtainPermanentIDsForObjects,那麼您收到的ID應該是永久性的(禁止框架中的任何實現錯誤,這對於此時的核心似乎不太可能)。