NSManagedObjectContextDidSaveNotification没用吗? [英] NSManagedObjectContextDidSaveNotification useless?

查看:213
本文介绍了NSManagedObjectContextDidSaveNotification没用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在使用Core Data了一段时间,并且使用了背景信息,并且想知道为什么每个人都建议使用 NSManagedObjectContextDidSaveNotification 从后台合并到主上下文。我使用一个 NSPersistentStoreCoordinator ,一个主上下文和一个背景上下文创建了一个测试项目。以下是初始化的代码片段:

 -(NSManagedObjectContext *)managedObjectContext {

if(_managedObjectContext != nil){
return _managedObjectContext;
}

NSPersistentStoreCoordinator * coordinator = [selfpersistentStoreCoordinator];
if(coordinator!= nil){
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}

return _managedObjectContext;
}



-(NSManagedObjectContext *)backgroundContext {
if(_backgroundContext!= nil){
return _backgroundContext;
}
_backgroundContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
_backgroundContext.persistentStoreCoordinator = self.persistentStoreCoordinator;

return _backgroundContext;
}

直到现在,我都会听这样的保存通知:

  [[NSNotificationCenter defaultCenter] addObserver:self 
选择器:@selector(mergeChanges :)
名称:NSManagedObjectContextDidSaveNotification
object:self.backgroundContext];

但是我意识到,如果我从该通知中合并也没关系。我可以编辑和保存其中的任何一个上下文,而另一个则可以在几秒钟后合并。



我的问题是,为什么我什至需要 NSManagedObjectContextDidSaveNotification

解决方案

您的上下文无关。它们都是连接到同一持久性存储协调器的根上下文。不需要处理 NSManagedObjectContextDidSaveNotification 通知。)



NSManagedObjectContextDidSaveNotification 在处理更复杂的上下文关系时很有用,因为中级上下文在更改时不会自动通知其所有子级。



例如,请查看Cadmium的架构图( https://github.com/jmfieldman/Cadmium )。当后台子上下文保存到writer上下文时,主上下文必须在主线程上处理 NSManagedObjectContextDidSaveNotification ,以合并更新。


I am using Core Data for a while now with a background contex, and was wondering why everyone advise to use the NSManagedObjectContextDidSaveNotification for merging from the background to the main context. I created a Test-Project with one NSPersistentStoreCoordinator, a main context and a background context. Here is the code fragment for initalisation:

- (NSManagedObjectContext *)managedObjectContext {

if (_managedObjectContext != nil) {
    return _managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
    _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
}

return _managedObjectContext;
}



- (NSManagedObjectContext *)backgroundContext {
if (_backgroundContext != nil) {
    return _backgroundContext;
}
_backgroundContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
_backgroundContext.persistentStoreCoordinator = self.persistentStoreCoordinator;

return _backgroundContext;
}

until now, i would have listened to the save notification like this:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(mergeChanges:)
                                             name:NSManagedObjectContextDidSaveNotification
                                           object:self.backgroundContext];

But i realised, it doesn't matter if I merge from that notification. I can edit and save either context, and the other one get's merged after some seconds itself.

So. my question, why would i even need the NSManagedObjectContextDidSaveNotification?

解决方案

Your contexts are not related. They are both root contexts attached to the same persistent store coordinator.

A change to the persistent store is automatically pushed to the root contexts associated with it (which is why you don't need to handle the NSManagedObjectContextDidSaveNotification notification.)

NSManagedObjectContextDidSaveNotification is useful when dealing with more complex context ancestry, since a mid-level context does not automatically notify all of its children when changed.

As an example, check out the architecture diagram for Cadmium (https://github.com/jmfieldman/Cadmium). When a background child context saves up to the writer context, the main context must handle a NSManagedObjectContextDidSaveNotification on the main thread to incorporate the updates.

这篇关于NSManagedObjectContextDidSaveNotification没用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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