Core Data 托管对象在重启模拟器之前看不到相关对象 [英] Core Data managed object does not see related objects until restart Simulator

查看:18
本文介绍了Core Data 托管对象在重启模拟器之前看不到相关对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个Stumper(至少对我来说).

Got a Stumper (at least for me).

我正在使用带有 ARC 的 iOS 5.0,以及 UIManagedDocument 中的 Core Data.

I am using iOS 5.0 w/ ARC, and Core Data inside of UIManagedDocument.

我有一个实体(组)与实体(人)之间存在一对多关系(称为人).当我添加一个新组,然后添加一个新人员(将人员的 .group 关系设置为新组)时,我无法使用 Person 实体上的谓词检索相关人员 where ("group == %@", myGroup).我也尝试使用 Group 的 addPerson 设置器.

I have an Entity (Group) with a to-many relationship (called people) to entity (Person). When I add a new Group, and add then a new Person (setting the person's .group relationship to the new group), I cannot retrieve the related people using a predicate on the Person entity where ("group == %@", myGroup). I also tried using the Group's addPerson setter.

如果我关闭 XCode 模拟器并重新运行它,它会识别上一次运行中创建的关系,我什至可以将新人添加到现有的 Group 对象中.我只是无法添加一个新组,然后在不关闭模拟器(或设备,如果我在设备上运行)的情况下添加人员以查看关系.

If I shut down XCode simulator and rerun it, it recognizes the relationship that was created in the previous run, I can even add new people to the existing Group object. I just can't add a new Group and then add people to without shutting down the Simulator (or device if I'm running on device) in order to see the relationship.

如果我在添加新组和相关人员后立即执行 [group.people count],它会给我正确的数字.但是在我重新启动应用程序之前,带有谓词的提取不起作用.

If I do a [group.people count], immediately after adding the new Group and a related Person, it gives me the correct number. But a fetch with a predicate doesn't work until I restart the app.

似乎 UIManagedDocument 的 managedObjectContext 没有看到这种关系.我试过保存上下文、保存 context.parentContext 和保存文档.这些都没有帮助.

It seems as if the managedObjectContext of the UIManagedDocument is not seeing the relationship. I've tried saving the context, saving context.parentContext, and saving the Document. None of that helped.

任何想法都将不胜感激!

Any ideas would be appreciated!

推荐答案

一个 UIManagedDocument 将保存,嗯,基本上,当感觉像它时;您无法控制何时发生.但是,它肯定会节省应用程序终止的时间,这就是您在重新启动时看到插入的原因.

A UIManagedDocument will save, well, basically, when it feels like it; you don't have control as to when that occurs. It will, however, certainly save on application termination, which is why you see the inserts when you restart.

因此,虽然您可能认为您拥有永久对象 ID,但它们实际上很可能是临时的,因此无法获取.只需通过 NSLog 转储它们即可验证这一点,因为在记录时会显示临时对象 ID.

As a result, while you may think you have permanent object ids, they're actually likely temporary, and thus can't be fetched. Simply dumping them via an NSLog would validate this, as temporary object ids show up as such when logged.

要强制它们永久存在并因此可用,请在执行添加后尝试以下操作.假设您有一个 UIManagedDocument 作为 ivar:

To force them to be permanent and thus usable, try the following after performing your additions. Assuming you have a UIManagedDocument as an ivar:

- (void)performUpdate
{
    NSManagedObjectContext * context = self.managedDocument.managedObjectContext;
    NSSet                  * inserts = [context insertedObjects];

    if ([inserts count])
    {
        NSError * error = nil;

        if ([context obtainPermanentIDsForObjects:[inserts allObjects]
                                            error:&error] == NO)
        {
            NSLog(@"BAM! %@", error);
        }
    }

    [self.managedDocument updateChangeCount:UIDocumentChangeDone];
}

显然,您可以在这里用更好的方法替换错误处理.UIManagedDocument 现在将在未来的某个时间保存(同样,您完全无法控制何时会发生,我们只是在最后一行要求它这样做,就像撤消管理器一样),但新插入的对象现在应该具有可用的永久 id,并且 fetch 应该可以正常工作.

Obviously, you'd replace the error handling with something a bit better here. The UIManagedDocument will now save at some point in the future (again, you have absolutely no control over when that'll happen, we're just asking it to do so in the last line, in the same way that an undo manager would), but newly-inserted objects should now have usable permanent ids and fetches should work properly.

是的,这对我来说也有点奇怪,但这似乎是使用 UIManagedDocument 做事的正确方法.

And yes, this seems a bit odd to me too, but this appears to be the correct way to do things with a UIManagedDocument in play.

坦率地说,我很想有人告诉我我错了并提供更好的解决方案.

Frankly, I'd love for someone to tell me I'm incorrect and provide a better solution.

这篇关于Core Data 托管对象在重启模拟器之前看不到相关对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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