不必要的对象出现在核心数据关系中 [英] unwanted objects appearing in core data relationship

查看:86
本文介绍了不必要的对象出现在核心数据关系中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长问题 - 提前感谢您的时间。保存新的托管对象后,我发现它们被添加到我的核心数据数据库中的另一个对象的关系中 - 我的代码调用没有setter方法,并且没有反向关系。我已经累了代码和使用日志隔离发生最好的我可以,但我遇到奇怪的行为,我不能解释(或修复)。

Long question---thanks in advance for your time. After saving new managed objects, I am finding them added to a relationship on another object in my core data database---one for which my code calls no setter method and that has no inverse relationship. I have pored over the code and used logs to isolate the occurrence the best I can, but I'm encountering bizarre behavior I cannot explain (or fix).

更多:

我有一个名为 PendingSyncTracker 的实体。它只有一个关系, objectsToSync 。我还没有在我的代码中添加任何行来调用这个关系上的setter方法。它是一个多对多的关系。它指向 BaseEntity 。对于反向选项,我选择了无反向关系。

I have an entity called PendingSyncTracker. It simply has one relationship, objectsToSync. I have not yet added any line in my code to call a setter method on this relationship. It is a to-many relationship. It points to BaseEntity. For the "Inverse" option, I have selected "No Inverse Relationship."

加载特定的表视图时,从服务器下载3个对象,然后解析为托管对象并保存。到表视图开始加载单元格时,这3个对象中的2个将神秘地出现在 objectsToSync 关系中。

When I load a particular table view, 3 objects are downloaded from a server and then parsed into managed objects and saved. By the time the table view begins loading cells, 2 of those 3 objects will mystifyingly be present in the objectsToSync relationship.

我使用 NSLog 在我的代码,以确定这些对象可以首先被发现作为 objectsToSync set。

I have used NSLog all over my code to figure out exactly when these objects can first be found as members of the objectsToSync set.

NSSet *objectsToSync = [[[SyncEngine sharedEngine] fetchClassNamed:@"PendingSyncTracker" withPredicates:nil][0] valueForKey:@"objectsPendingSync"];
NSLog(@"PendingSyncTracker objectsToSync set (%lu objects): %@", (unsigned long)[objectsToSync count], objectsToSync);

当他们第一次出现在集合中时的答案实际上取决于我在哪里/不要放置这两行代码!

The answer to when they first appear in the set actually varies depending on where I do/don't place those 2 lines of code!


  • 对象上下文保存在保存我的3个新的核心数据对象的过程中。

  • The objects are never found on the relationship before the managed object context is saved in the course of saving my 3 new core data objects.

如果我不使用这两行,表视图将新对象发送到同步引擎以存储在本地(其中访问和保存MOC)的表视图控制器,然后日志将显示有2个对象已添加到关系中。

If I don't use those 2 lines till I'm back in the Table View Controller that sent the new objects off to the Sync Engine to be stored locally (where the MOC is accessed and saved), then the log will there reveal that 2 objects have been added to the relationship.

如果我在同步引擎中保存MOC之后立即使用这两行,那么日志将指示(在TVC中和在后面)只有1个对象被添加到关系。

If I use those 2 lines immediately after saving the MOC in the Sync Engine, then the logs will indicate (both there and back in the TVC) that only 1 object has been added to the relationship.

如果我在保存MOC之前和之后立即使用这两行(然后回到TVC),那么所有3个日志都会显示关系包含一个空集。

If I use those 2 lines immediately before and after saving the MOC (and back in the TVC), then all 3 logs will reveal that the relationship contains an empty set.

我也在 cellForRowAtIndexPath 的开头有这两行。无论以前的日志,该日志将始终指示已将两个对象添加到关系中。

I also have those 2 lines at the beginning of cellForRowAtIndexPath. Regardless of prior logs, that log will always indicate that 2 objects have been added to the relationship.

在同步引擎中创建的托管对象作为实体类型存储为 BaseEntity 的子实体( objectsToSync 关系点)。添加到关系中的两种类型都被定义为具有互逆关系,但是具有不同的对象,而不是 PendingSyncTracker (尽管不同的对象是BaseEntity的子实体! )。

All 3 of the managed objects that are created in the Sync Engine are stored as entity types that are subEntities of BaseEntity (to which the objectsToSync relationship points). The 2 types that get added to the relationship are each defined to have a reciprocal relationship, but with a different object, not PendingSyncTracker (although the different object is a subEntity of BaseEntity!).

那么,什么解释了这些意见?如何将这些对象添加到关系中?

So.. what explains these observations? How are these objects getting added to the relationship?

更新:

- (NSArray*) fetchClassNamed:(NSString*)className withPredicates:(id)parameters;
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:className inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // set predicates
    if (!(parameters == nil)) {
        [fetchRequest setPredicate:parameters];
    }

    NSError *error;
    NSArray *fetchedResults = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

    return fetchedResults;
}


推荐答案

c $ c> [[[SyncEngine sharedEngine] fetchClassNamed ... do?

First, what does [[[SyncEngine sharedEngine] fetchClassNamed... do? Just a guess but it is doing something with KVC to set the relationship for you.

此外,您应始终 总是具有反比关系。即使您从不使用它,Core Data也是如此。没有反向关系可能会导致很多问题,包括但不限于性能问题和潜在的数据损坏。

Also, you should always, always, always have an inverse relationship. Even if you never use it, Core Data does. Not having an inverse can lead to lots of issues, including but not limited to performance problems and potentially data corruption.

添加一个反向关系,并更新您的问题与 -fetchClassNamed ...

Add an inverse relationship and update your question with what -fetchClassNamed... does.

这篇关于不必要的对象出现在核心数据关系中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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