核心数据一对多关系在获取新对象后失去了他的关系

时间:2013-10-24 09:57:01

标签: ios objective-c core-data restkit nsfetchrequest

我问过这个问题before。但我正在开一个新的,因为我现在有一些其他的见解。首先,这是core data model的样子。

enter image description here

现在我将第一个appointments提取到我的模型中。一切都运转正常。但是当我加载新约会时问题就出现了。然后,先前的约会位置关系转到NULL。奇怪的是location relationship仅适用于最后加载的约会。

enter image description here

我正在使用restkit将我的JSON映射到我的core-data model。这就是我建立关系的方式。

 [locationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"appointments" toKeyPath:@"appointments" withMapping:appointmentMapping]];

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

首先,你的模型很糟糕(没有冒犯)。您应该创建LabelData,Data和VerplichtData实体。这些应与位置/约会有一对一的关系。位置和约会应该与LabelData,Data和VerplichtData有很多关系。

你应该遵循Mundis的建议而不是使用休息套件,它可能会使调试更轻松。 Apple has a pretty decent strategy for importing data in a smart way(即快速且无重复)。这里是文档的复制粘贴,以防链接死亡:

有效实施查找或创建 导入数据时的常用技术是遵循“查找或创建”模式,您可以在其中设置一些数据,从中创建托管对象,确定托管对象是否已存在,如果不存在则创建托管对象。 在许多情况下,您可能需要查找一组离散输入值的现有对象(已存储在商店中的对象)。一个简单的解决方案是创建一个循环,然后为每个值依次执行一个fetch以确定是否存在匹配的持久化对象,依此类推。这种模式不能很好地扩展。如果使用此模式分析应用程序,通常会发现fetch是循环中较昂贵的操作之一(与仅迭代项目集合相比)。更糟糕的是,这种模式将O(n)问题转化为O(n ^ 2)问题。 在可能的情况下,在单次传递中创建所有托管对象,然后在第二次传递中修复任何关系,效率会更高。例如,如果导入您知道不包含任何重复项的数据(例如,因为您的初始数据集为空),则可以创建托管对象来表示您的数据,而不进行任何搜索。或者,如果导入没有关系的“平面”数据,则可以为整个集创建托管对象,并在使用单个大型IN谓词进行保存之前清除(删除)任何重复项。 如果您确实需要遵循查找或创建模式 - 比如因为您要导入异构数据,其中关系信息与属性信息混合在一起 - 您可以通过将获取的数量减少到最少来优化查找现有对象的方式执行。如何完成此操作取决于您必须使用的参考数据量。如果要导入100个潜在的新对象,并且在数据库中只有2000个,则获取所有现有对象并缓存它们可能不会造成重大损失(特别是如果您必须多次执行该操作)。但是,如果数据库中有100,000个项目,那么保留这些项目的内存压力可能会非常高。 您可以使用IN谓词和排序的组合来减少对单个提取请求使用Core Data的过程。例如,假设您要获取员工ID列表(作为字符串),并为尚未存在于数据库中的所有员工创建员工记录。请考虑此代码,其中Employee是具有name属性的实体,listOfIDsAsString是要添加对象(如果它们尚不存在于商店中)的ID列表。 首先,对感兴趣的ID(字符串)进行分类和排序。

// get the names to parse in sorted order
NSArray *employeeIDs = [[listOfIDsAsString componentsSeparatedByString:@"\n"]
    sortedArrayUsingSelector: @selector(compare:)];
Next, create a predicate using IN with the array of name strings, and a sort descriptor which ensures the results are returned with the same sorting as the array of name strings. (The IN is equivalent to an SQL IN operation, where the left-hand side must appear in the collection specified by the right-hand side.)

// Create the fetch request to get all Employees matching the IDs.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:
    [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:aMOC]];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat:@"(employeeID IN %@)", employeeIDs]];

// make sure the results are sorted as well
[fetchRequest setSortDescriptors:
    @[[[NSSortDescriptor alloc] initWithKey: @"employeeID" ascending:YES]]];
Finally, execute the fetch.

NSError *error;
NSArray *employeesMatchingNames = [aMOC executeFetchRequest:fetchRequest error:&error];
You end up with two sorted arrays—one with the employee IDs passed into the fetch request, and one with the managed objects that matched them. To process them, you walk the sorted lists following these steps:

Get the next ID and Employee. If the ID doesn't match the Employee ID, create a new     Employee for that ID.
Get the next Employee: if the IDs match, move to the next ID and Employee.
Regardless of how many IDs you pass in, you only execute a single fetch, and the rest is just walking the result set.

下面的列表显示了上一节中示例的完整代码。

// Get the names to parse in sorted order.
NSArray *employeeIDs = [[listOfIDsAsString componentsSeparatedByString:@"\n"]
    sortedArrayUsingSelector: @selector(compare:)];

// create the fetch request to get all Employees matching the IDs
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:
    [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:aMOC]];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat: @"(employeeID IN %@)",     employeeIDs]];

// Make sure the results are sorted as well.
[fetchRequest setSortDescriptors:
@[ [[NSSortDescriptor alloc] initWithKey: @"employeeID" ascending:YES] ]];
// Execute the fetch.
NSError *error;
NSArray *employeesMatchingNames = [aMOC executeFetchRequest:fetchRequest error:&error];
相关问题