Core Data管理对象在重新启动Simulator之前不会看到相关对象 [英] Core Data managed object does not see related objects until restart Simulator

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

问题描述

有一个stumper(至少对我来说)。



我使用iOS 5.0 w / ARC和Core Data在UIManagedDocument。



我有一个实体(组)与一个对多关系(称为人)到实体(Person)。当我添加一个新的组,然后添加一个新的人(设置人的.group关系到新组),我不能检索相关的人使用一个谓词Person实体where(group ==%@,myGroup )。我也尝试使用Group的addPerson setter。



如果我关闭XCode模拟器并重新运行它,它会识别在上一次运行中创建的关系,我甚至可以添加新的人到现有的Group对象。我只是不能添加一个新的组,然后添加人,而不关闭模拟器(或设备,如果我在设备上运行)为了看到的关系。



如果我做一个[group.people count],在添加新组和一个相关Person之后,它会给我正确的数字。但是,在我重新启动应用程序之前,使用谓词的提取不会工作。



似乎UIManagedDocument的managedObjectContext没有看到关系。我已经尝试保存上下文,保存context.parentContext,并保存文档。

解决方案

任何想法都可以帮助。



< > UIManagedDocument会保存,好吧,基本上,当它感觉像它;你没有对何时发生的控制。



因此,虽然你可能认为你有永久的对象id ,它们实际上可能是临时的,因此无法获取。只要通过NSLog转储它们就可以验证这一点,因为临时对象id在记录时会显示出来。



要强制它们永久使用,执行您的添加。假设你有一个UIManagedDocument作为一个ivar:

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

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

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

[self.managedDocument updateChangeCount:UIDocumentChangeDone];
}

显然,你会在这里更好一些。 UIManagedDocument现在将在未来的某个时间保存(同样,你完全不能控制什么时候发生,我们只是要求它在最后一行这样做,就像undo管理器那样) ,但新插入的对象现在应该具有可用的永久ID和抓取应该正常工作。



是的,这似乎有点奇怪我,但这似乎是正确的做法是使用UIManagedDocument进行操作。



坦率地说,我希望有人告诉我我不正确,并提供更好的解决方案。 p>

Got a Stumper (at least for me).

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

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.

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.

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.

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!

解决方案

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.

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.

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];
}

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.

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管理对象在重新启动Simulator之前不会看到相关对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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