神奇的记录,保存和NSFetchedResultsController [英] Magical Record, saving, and NSFetchedResultsController

查看:110
本文介绍了神奇的记录,保存和NSFetchedResultsController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道这是否是Magical Record做的保存方式的问题,或者我只是在某个地方发生错误。

Not sure if this is an issue with the way Magical Record does saves, or I'm just making an noob mistake somewhere.

我使用NSFetchedResultController (FRC)和UITableView显示实体列表,当用户点击添加具有编辑器的View Controller时,使用 [MyEntity MR_createEntity] 。用户可以在这里添加通过关系添加到主实体的附加实体。当用户在此View Controller中点击Save时,使用 [[NSManagedObjectContext MR_contextForCurrentThread] MR_save]

I'm using an NSFetchedResultController (FRC) and UITableView to display a list of entities, when the user taps "Add" a new View Controller with an editor is pushed, a new entity is created with [MyEntity MR_createEntity]. The user can add additional entities here that are added to the main entity via a relationship. When the user taps "Save" in this View Controller the context is saved using [[NSManagedObjectContext MR_contextForCurrentThread] MR_save]

NSFetchedResultsController看起来更新,但是当我点击编辑实体时,没有子实体存在。调试似乎表明,即使实体已保存,FRC仍然具有其临​​时ID的实体。

The NSFetchedResultsController appears to update, but when I tap to edit the entity none of the child entities are there. Debugging seems to show that even though the entity has been saved the FRC still has the entity with it's temporary ID.

我在FRC controllerDidChangeContent中做了一个天真的 [self.tableView reloadData]

重新启动应用程序将加载正确的实体,子实体在编辑器视图控制器中正确显示。

Restarting the application loads the correct entities and the child entities are show properly in the editor view controller.

看起来FRC响应主线程保存事件,但保存实际上发生在后台线程上,因此FRC不会看到它。我检查了所有的我的操作(设置FRC,创建和获取实体)都发生在主线程上下文。

It looks like the FRC responds to the "main thread" save event, but the save is actually happening on a background thread so the FRC doesn't see it. I've checked and all "my" operations (setting up the FRC, creating and fetching entities) are all happening on the main thread context.

我试过监听更改通知MR_rootSavingContext并将它们与主线程上下文合并,这种工作,但我结束了重复行在FRC(一个是正确的永久实体,一个是临时实体)。

I tried listening for change notifications on MR_rootSavingContext and merging them with the main thread context, which sort of worked but I ended up with duplicates rows in the FRC (one was the correct "permanent" entity and one was the temporary one).

推荐答案

确定,我不知道这是正确的方式做到,但我发现,如果我创建了我的NSFetchedResultsController在MR_rootSavingContext中,而不是使用inContext版本 MR_fetchAllSortedBy 的默认上下文。

OK, I'm not sure if this is "the right way to do it" but I've found that it works correctly if I created my NSFetchedResultsController in the MR_rootSavingContext instead of the default context using the "inContext" version of MR_fetchAllSortedBy.

从FRC现在正在观看rootSavingContext而不是其子节点之一的角度来看。仍然,我会想,因为我在同一个线程做所有我的操作,这不会是一个问题。

I guess this makes sense from the point of view that the FRC is now watching the rootSavingContext instead of one of it's children. Still, I would have thought since I'm doing all my operations on the same thread that wouldn't be an issue.

更新:这个方法唯一的困扰是,如果我只是使用 [frc objectAtIndexPath:] 获取实体给它的编辑视图控制器,那么它不再在默认上下文。通过使用NSManagedObjectContext的 existingObjectWithID 在默认上下文中重新获取实体来解决此问题。仍然不是所有感觉都很对,但它为我工作。

Update: The only gotcha with this approach is that if I just grab the entity using [frc objectAtIndexPath:] to give it to the editing view controller then it's no longer in the default context. Worked around this by re-fetching the entity in the default context using NSManagedObjectContext's existingObjectWithID. Still doesn't all feel quite right, but it's working for me.

这篇关于神奇的记录,保存和NSFetchedResultsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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