2013-03-17 59 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