2013-03-17 27 views
1

我試圖將現有數據從本地存儲播種到啓用iCloud的存儲。當我嘗試從本地管理對象訪問的關係對象,以下異常被拋出,從本地持久性存儲向iCloud存儲區播種數據時的管理關係

> illegal attempt to establish a relationship between objects in 
> different contexts 

這就是我要做的。在提前

NSEntityDescription *entity = [recurringExpense entity]; 
    RecurringExpense *newRecExpense = [[RecurringExpense alloc]initWithEntity:entity insertIntoManagedObjectContext:moc]; 
    newRecExpense.category = recurringExpense.category; 
    [moc assignObject:newRecExpense toPersistentStore:store]; 

感謝。

+0

哪裏'category'對象創建?錯誤消息表明它屬於不同的上下文。 – 2013-03-17 21:17:41

+0

'recurringExpense'來自不同的上下文。 P.S. 我改變了我的計劃並打算使用[https://github.com/alekseyn/iCloudStoreManager](https://github.com/alekseyn/iCloudStoreManager)來處理種子和管理商店。 – 2013-03-18 12:25:32

回答

0

您似乎遇到了在對象之一的管理對象上下文之外或跨線程/隊列邊界創建關係的問題。

根據Apple文檔,「您必須在其將使用的線程上創建託管上下文。」此外,關係兩邊的對象需要在同一個NSManagedObjectContext中引用。

從蘋果:

參見核心數據併發這裏:http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html

下面的堆棧溢出的答案提供了更多的細節:https://stackoverflow.com/a/1554935/1294009