- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个addingManagedObjectContext 作为新实体的便笺簿区域,然后在“保存”时将新实体合并到我的主 ManagedObjectContext 中,类似于 CoreDataBooks 示例中的显示方式。
合并新实体后,如何快速引用它以用于显示详细 View ?
您是否必须使获取结果 Controller 退出并再次执行获取(如 CoreDataBooks 代码中所述,昂贵)?我假设对象在“addingManagedObjectContext”中的初始 id 在合并后不会保持不变。
在 Recipes 项目中,不存在这种问题,因为您正在一个 ManagedObjectContext 中创建和使用新实体。因此,您可以引用新创建的项目来显示其详细 View 。
来自 CoreDataBooks:
/**
Add controller's delegate method; informs the delegate that the add operation has completed, and indicates
whether the user saved the new book.
*/
- (void)addViewController:(AddViewController *)controller didFinishWithSave:(BOOL)save {
if (save) {
/*
The new book is associated with the add controller's managed object context.
This is good because it means that any edits that are made don't affect the
application's main managed object context -- it's a way of keeping disjoint edits
in a separate scratchpad -- but it does make it more difficult to get the new book
registered with the fetched results controller.
First, you have to save the new book. This means it will be added to the persistent
store. Then you can retrieve a corresponding managed object into the application
delegate's context. Normally you might do this using a fetch or using objectWithID: -- for example
NSManagedObjectID *newBookID = [controller.book objectID];
NSManagedObject *newBook = [applicationContext objectWithID:newBookID];
These techniques, though, won't update the fetch results controller, which
only observes change notifications in its context.
You don't want to tell the fetch result controller to perform its fetch again
because this is an expensive operation.
You can, though, update the main context using mergeChangesFromContextDidSaveNotification: which
will emit change notifications that the fetch results controller will observe.
To do this:
1 Register as an observer of the add controller's change notifications
2 Perform the save
3 In the notification method (addControllerContextDidSave:), merge the changes
4 Unregister as an observer
*/
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc addObserver:self selector:@selector(addControllerContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:addingManagedObjectContext];
NSError *error;
if (![addingManagedObjectContext save:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
[dnc removeObserver:self name:NSManagedObjectContextDidSaveNotification object:addingManagedObjectContext];
}
// Release the adding managed object context.
self.addingManagedObjectContext = nil;
// Dismiss the modal view to return to the main list
[self dismissModalViewControllerAnimated:YES];
}
/**
Notification from the add controller's context's save operation. This is used to update the
fetched results controller's managed object context with the new book instead of performing
a fetch (which would be a much more computationally expensive operation).
*/
- (void)addControllerContextDidSave:(NSNotification*)saveNotification {
NSLog(@"addControllerContextDidSave");
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
// Merging changes causes the fetched results controller to update its results
[context mergeChangesFromContextDidSaveNotification:saveNotification];
}
最佳答案
看看这些评论,看起来他们正在谈论获取结果 Controller 。因此,在您刚刚更改一个对象后,让 FRC 执行新的获取会相当昂贵,因此您可以将添加上下文与其合并以通知它任何更改。
执行保存和合并后,您在添加上下文中引用的任何托管对象将不再具有临时对象 ID,因为它们存在于持久存储中。因此,您可以记住 ID 并在主应用程序上下文中使用 [applicationContext objectWithID:newBookID] 就可以很好地获取您正在查找的对象的句柄。这将返回应用程序上下文中的对象(及其所有更改)。
合并后,该对象很可能存在于内存中,无需访问存储。然而,即使是这样,因为您只处理要在详细 View 中显示的单个对象,所以根本不是问题。访问存储而不是上下文内存速度较慢,但显然必须在应用程序期间发生很多次,并且除非您处理大量数据,否则不太可能导致问题!
希望这有帮助!
关于iphone - CoreData 和 mergeChangesFromContextDidSaveNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1429900/
我在创建几百个托管对象的单独线程中有一个托管对象上下文,当它保存时,将保存通知传递给主线程,并且我的其他上下文(在主线程上)被更新: 在线程 [ApplicationDelegate performS
我有两个 NSManagedObjectContext,一个用于 ui,一个用于后台任务。每当背景更改时,我都试图将更改合并到 UIcontext。但是每当我调用 mergeChangesFromCo
我正在创建一个addingManagedObjectContext 作为新实体的便笺簿区域,然后在“保存”时将新实体合并到我的主 ManagedObjectContext 中,类似于 CoreData
当我在内存类型 nsmanagementobjectcontext 中使用时,我的应用程序(osx 10.8.3)的内存大小有所增加: 波纹管是仪器的前 2 个函数。 我确实清除了内存问题,但泄漏仍然
我们一直在尝试调试 Core Data 多上下文/线程问题,其中将 Core Data 保存通知合并到我们的主线程 NSManagedObjectContext 偶尔会使应用程序崩溃。这导致大约 2%
我正在编写一个很大程度上基于 CoreData 的 iPad cooking 应用程序。 在我的应用程序中,我有一个用于编辑配方的屏幕,其中包含 2 种类型的信息: 带有配方类别\子类别和国家/地区的
在我的应用程序中,我调用服务器 API 来获取用户列表。我在带有 NSFetchedResultsController 的 UITableView 中显示这些用户。一些用户与当前连接的用户保持联系,而
我正在为一个更大的 iOS 应用程序编写一个 Core Data ContextManager。 ContextManager 提供一个 NSManagedContext,当其他 ContextMan
iOS 上的 Core Data 有一些真正奇怪的行为。 我的主线程有一个 NSManagedObjectContext,用于从 SQLLite 持久存储中读取数据并将其显示给用户。我还有一个由 NS
[编辑:问题的简化版本] mainMOC 是主要的托管对象上下文 editorMOC 是在 editorViewController 中创建的托管对象上下文,带有撤消管理器,因此用户可以编辑单个托管对
我正在使用 KVO 观察 NSManagedObject 的变化。我正在观察的 NSManagedObject 是主队列上 NSManagedObject 上下文的一部分。 当我在后台(私有(priv
我在我的应用程序中广泛使用 CoreData 来查看产品。所有数据同步都在单独的 NSManagedObjectContexts 后台完成,当更改同步完成时,我调用 mergeChangesFromC
我得到 uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault
我在 CoreData 中遇到了一些奇怪的行为,导致我的一个 MOC 最终处于不一致的状态。我在一个小样本中复制了这个问题 project 这是我的情况的基本概要: 我有两种实体类型,管道和盒子。每个
我正在使用私有(private)托管对象上下文在持久存储中创建一些新对象,然后在保存私有(private) MOC 后,使用 mergeChangesFromContextDidSaveNotific
一直在浏览与此类似的帖子,但没有一个回答我的问题。 与 CoreDataBooks 一样,我使用单独的 MOC 来添加和编辑项目并观察正确的通知,如下所示: - (void)addController
首先是背景: 我有一个单例,可以在上面做我所有与 CoreData 相关的事情。它被多个线程调用。对于每个线程,我始终使用相同的 StoreCoordinator 生成一个新的 MOC。我有两个 fe
好消息: 我从服务器下载数据,并通过 Core Data 线程限制保存数据,当后台 MOC 被保存时,主 MOC 被合并。 所有保存操作正常 此外,MOC 的合并没有任何问题 我正在寻找的错误: 当我
我有一个设置,在后台线程中我将新对象保存在 managedObjectContext A 中,在 TableViewController 中我听取 NSManagedObjectContextDidS
我正在使用后台线程执行一项长时间运行的任务,包括从服务中提取数据并通过 CoreData 将记录插入数据库。正如这里的许多答案所建议的那样,我正在观察 NSManagedObjectContextDi
我是一名优秀的程序员,十分优秀!