使用子/父NSManagedObjectContext时的奇怪行为 [英] Strange behavior when using child/parent NSManagedObjectContext

查看:132
本文介绍了使用子/父NSManagedObjectContext时的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我需要在后台线程上计算东西(多秒操作)和写东西(与服务器同步数据)。



因为这个我使用两个NSManagedObjectContext(MOC),一个孩子和一个父,并且这些必须总是同步。为了确保他们是同步的我总是编辑/添加数据到子MOC,所以它推送到主MOC与以下模式:

  [childMOC performBlock:^ {

MyObject * myObject = *在childMOC中创建新对象*

[childMOC save:& error];

[mainMOC performBlock:^ {
[mainMOC save:& error];
//这是否必须使其正常工作?
// [childMOC performBlock:^ {
// [childMOC refreshObject:myObject mergeChanges:NO];
//}];
}];
}];

过一段时间后,我在背景环境中似乎有两个版本的相同对象,临时ID和具有永久ID的临时ID。如果我添加子对象到真正的(通过将新的永久ID从主要传递给子MOC)我没有看到这些对象,当我在后台MOC中检索我的对象,因为它是缓存的旧临时对象。 p>

我已经看到上面的模式被使用了很多,但似乎很奇怪,没有人提到这个临时/永久ID问题。


  1. 在上下文中它可以是同一个对象的两个版本是不正确的。如果我将NSManagedObjectID传递给子MOC并检索,那么子MOC是否应该更新我的现有对象,而不是创建一个新的对象,并将旧的临时对象作为缓存的默认对象?


  2. 我需要在每个创建对象的地方使用已注释的行吗?


  3. 或者也可以使用mergeChangesFromContextDidSaveNotification相同的效果?



解决方案



1)使用后台MOC作为父MOC,将主MOC用作子。作为奖励,我不需要保存主MOC来获取永久ID。

  [DC.backgroundMOC performBlock: {
//添加,保存和更新托管对象
[DC saveContext]; //更改被推送到主上下文
}];

2)使用NSManagedObjectContextDidSaveNotification保持主MOC为最新(主MOC更新UI)

   - (void)backgroundMOCSaved:(NSNotification *)notification {
[mainMOC performBlock:^ {
[mainMOC mergeChangesFromContextDidSaveNotification:notification];
}];
}


I'm developing an application where I need to both calculate things (multiple seconds operations) and write things (sync data with a server) on a background thread.

Because of this I use two NSManagedObjectContexts (MOC), a child and a parent, and these must always be in sync. To make sure they are in sync I always edit/add data to the child MOC so it gets pushed to the main MOC with the following pattern:

[childMOC performBlock:^{

    MyObject *myObject = *create new object in childMOC*

    [childMOC save:&error];

    [mainMOC performBlock:^{
        [mainMOC save:&error];
        // Is this mandatory to make it work correctly?
        // [childMOC performBlock:^{
        //     [childMOC refreshObject:myObject mergeChanges:NO];
        // }];
    }];
}];

After a while I seemed to have two versions of the same object in the background context, one with a temporary ID and one with a permanent ID. If I e.g. added child objects to the "real" one (by passing fresh permanent ID from main to child MOC) I didn't see these objects when I retrieved my object in the background MOC cause it is the old temporary one that is cached.

I've seen the pattern above been used a lot, but it seems strange that no one mentioned this temporary/permanent ID problem.

  1. It doesn't feel right that it can be two versions of the same object within a context. If I pass an NSManagedObjectID to the child MOC and retrieve that, shouldn't the child MOC update my existing object instead of creating a new one and leave my old temporary as cached default?

  2. Do I need to use the commented row on each place I create an object?

  3. Or maybe it works with mergeChangesFromContextDidSaveNotification, will that give the same effect?

解决方案

My solution was to:

1) Use the background MOC as the parent MOC and the main MOC as a child. As a bonus I don't need to save the main MOC to get the permanent IDs.

[DC.backgroundMOC performBlock:^{
    // Add, save and update managed objects
    [DC saveContext]; // The changes is being pushed to the main context
}];

2) Use NSManagedObjectContextDidSaveNotification to keep the main MOC up to date (the main MOC is updating the UI)

- (void) backgroundMOCSaved:(NSNotification*)notification {
    [mainMOC performBlock:^{
        [mainMOC mergeChangesFromContextDidSaveNotification:notification];
    }];
}

这篇关于使用子/父NSManagedObjectContext时的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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