Core Data To-Many关系向父实体添加对象时创建重复项 [英] Core Data To-Many Relationship Creating Duplicates When Adding Object to Parent Entity

查看:154
本文介绍了Core Data To-Many关系向父实体添加对象时创建重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Core Data和objective-c的新手。我正在一个项目,我从一个Web服务获取JSON数据,并将其与核心数据同步。
我成功地遵循了教程,并能够获得JSON到核心数据没有任何问题。
我遇到问题是更新与一对多关系相关联的NSSet。到目前为止,我可以得到它更新,但这样做它创建集合中的重复条目。例如,我尝试在我的自定义ManagedObject Entity1中使用Xcode生成的访问方法:

I am new to Core Data and objective-c. I am working on a project where I am taking JSON data from a web service and syncing it with core data. I successfully followed this tutorial and am able to get the JSON into core data without any problem. Where I am having trouble is updating the NSSet associated with a to-many relationship. So far I can get it to update but in doing so its creating duplicate entries in the set. For example, I tried using the Xcode generated access method in my custom ManagedObject Entity1:

Entity1<-->>Entity2<-->>Entity3



我使用此代码将Entity2对象添加到Entity1



I use this code to add the Entity2 object to Entity1

    NSNumber *parentIdNumber = [record valueForKey:@"parent_id"];
    NSArray *parentIdArray = [NSArray arrayWithObject:parentIdNumber];
    NSArray *parentEntityArray = [self managedObjectsForClass:@"Entity1" sortedByKey:@"id" usingArrayOfIds:parentIdArray inArrayOfIds:YES];
    Entity1 *parentEntity = [parentEntityArray lastObject];
    [parentEntity addEntity2Object:(Entity2 *)newManagedObject];

在运行时查看每个变量后,我确定一切正常工作,直到最后一行。当我添加一个Entity2到Entity1它事实上添加的对象。但是,当我尝试并添加3个不同的Entity2对象,然后它似乎在Entity1中创建3个重复的Entity2对象。这3个重复是添加的Entity2的最后一个实例。

After looking at each variable at runtime, I have determined that everything is working correctly up until the last line. When I add an Entity2 to Entity1 it does in fact add the object. However, when I try and add 3 different Entity2 objects then it appears to create 3 duplicate Entity2 objects in Entity1. The 3 duplicates are of the last instance of Entity2 added.

我也尝试使用从这个答案的方法:http://stackoverflow.com/a/5370758/2670912 。其格式如下:

I have also tried using the approach from this answer: http://stackoverflow.com/a/5370758/2670912. Which looks like this:

    NSNumber *parentIdNumber = [record valueForKey:@"parent_id"];
    NSArray *parentIdArray = [NSArray arrayWithObject:parentIdNumber];
    NSArray *parentEntityArray = [self managedObjectsForClass:@"Entity1" sortedByKey:@"id" usingArrayOfIds:parentIdArray inArrayOfIds:YES];
    Entity1 *parentEntity = [parentEntityArray lastObject];
    NSMutableSet *entity2Set = [parentEntity mutableSetValueForKey:@"entity2"];
    [entity2Set addObject:newManagedObject];

这具有相同的重复条目结果,除了获取添加的第三个对象的3个重复条目,I

This has the same duplicate entry result except instead of getting 3 duplicate entries of the 3rd object added, I get 3 duplicate entries of the first object added.

有没有人知道什么造成这种情况?

Does anyone have any idea whats causing this?

推荐答案

我使用的代码片段运行正常。问题是当我在 UITableView 中显示数据时,它正确地整理了 NSSet 中的实体数量显示正确的细胞数量。但是,我有一个代码:

The snippit of code I was using turned out to be working correctly. The problem was when I was displaying the data in a UITableView it was correctly graping the number of entities in the NSSet thus showing the correct number of cells. However, I had an code:

    NSSet *entitiesSet = [self.selectedEntity1 valueForKey:@"entity2"];
    NSArray *entities = [entitiesSet allObjects];
    Entity2 *entity = [entities objectAtIndex:[indexPath indexAtPosition:0]];

这导致单元格只显示 NSSet 导致它看起来像重复。我通过将 0 更改为 1 来修复此问题:

This cause the cell to only display the last object in the NSSet causing it to look like duplicates. I fixed the problem by changing the 0 to a 1 like this:

    Entity2 *entity = [entities objectAtIndex:[indexPath indexAtPosition:1]];

这篇关于Core Data To-Many关系向父实体添加对象时创建重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆