NSManagedObjectContext的mergeChangesFromContextDidSaveNotification:post NSManagedObjectContextDidSaveNotification?

时间:2013-04-25 04:17:27

标签: objective-c core-data nsmanagedobjectcontext

我有一堆NSOperations,他们自己的NSManagedObjectContexts对我的Core Data存储进行了更改并保存并成功将其更改到主线程的NSManagedObjectContext中。我知道这个。现在我希望在主要上下文更新时通知前UIViewController。所以我......

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(refreshData:)
                                                 name:NSManagedObjectContextDidSaveNotification
                                               object:[NSManagedObject mainThreadManagedObjectContext]];
}

但是我不认为在合并后台线程的更改后,主线程的NSManagedObjectContext会发布自己的任何通知。我试图在文档中的哪个地方找到Apple说我应该在告诉主要线程合并更改或其他一些之后发布我自己,但没有运气。在我的NSOperation中有额外的参考,我有......

+ (void)mergeChanges:(NSNotification *)notification
{
    NSManagedObjectContext *managedObjectContext = [self mainThreadManagedObjectContext];

    [managedObjectContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
                                           withObject:notification
                                        waitUntilDone:YES];
}

+ (NSManagedObjectContext *)adHocManagedObjectContext
{
    NSManagedObjectContext *adHocManagedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] adHocManagedObjectContext];

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

    return adHocManagedObjectContext;
}

我是否希望听到主线程的NSManagedObjectContext有关它的保存,或者我应该在mergeChangesFromContextDidSaveNotification后发布我自己的通知:?

1 个答案:

答案 0 :(得分:5)

mergeChangesFromContextDidSaveNotification:未发布NSManagedObjectContextDidSaveNotification,因为mergeChangesFromContextDidSaveNotification:没有告诉要保存的上下文。

也许您的前线UIViewController应该观察NSManagedObjectContextObjectsDidChangeNotification