【发布时间】:2011-09-21 13:45:11
【问题描述】:
我正在从 Web 服务导入数据,在这些数据中我有一个类别树。
例如:
餐厅
- 意大利语
- 墨西哥
- 牛排馆
在我的模型中,我设置了一个名为 ParentCategory 的关系和一个名为 Subcategories 的逆关系。到目前为止一切都很好。
现在,当我导入我的类别时,我正在做这样的事情:(注意:我的代码看起来不像这样。我已经更改了它以举例说明我的问题)
NSEntityDescription *categoryEntity = [NSEntityDescription entityForName:@"Categories" inManagedObjectContext:context];
NSDescription *parentCategory = [NSEntityDescription insertNewObjectForEntityForName:[categoryEntity name] inManagedObjectContext:context]
//Then I actually set some values for parentCategory.
//Then create the subcategories.
NSDescription *italianDescription = [NSEntityDescription insertNewObjectForEntityForName:[categoryEntity name] inManagedObjectContext:context];
[italianDescription objectForKey:@"Italian" forKey:@"name"];
//Set the relationship
[italianDescription objectForKey:parentCategory forKey:@"ParentCategory"];
//All this works great but then when
NSEntityDescription *mexicanDescription = [NSEntityDescription insertNewObjectForEntityForName:[categoryEntity name] inManagedObjectContext:context];
[mexicanDescription objectForKey:@"Mexican" forKey:@"name"];
[mexicanDescription objectForKey:parentCategory forKey:@"ParentCategory"];
似乎当最后一行代码运行时,它从“italianDescription”中删除了关系,因此在我保存我的上下文后,它显示它没有父类别。我从我的 Web 服务中删除了一些数据,并且只有我设置与父类别对象 X 的关系的最后一个子类别保留了它。以前的所有内容都会丢失它。
我查看了苹果开发者文档,但并没有什么帮助。话虽如此,我该如何克服呢?或者用关系导入它的有效方法是什么。
【问题讨论】:
-
请在您的问题中添加 - 您的 Category 实体的定义,以及您的实际代码。
标签: objective-c ios core-data core-data-migration