I have a singleton name CoreDataManager
which register the mergeContextChangesForNotification
in it:
+ (id) sharedManager{
static CoreDataManager *mSharedManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
mSharedManager = [[CoreDataManager alloc] init];
});
return mSharedManager;
}
- (id)init
{
self = [super init];
if (self) {
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mergeContextChangesForNotification:)
name:NSManagedObjectContextDidSaveNotification
object:nil];
});
}
return self;
}
after i receive the notification:
- (void)mergeContextChangesForNotification:(NSNotification *)notification {
[shareContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
withObject:notification
waitUntilDone:YES];
}
I have two question here:
- Should i use
performSelectorOnMainThread
here? since this answer says never.should i change it to GCD and usedispatch_get_main_queue
?? - Does the registration of
mergeContextChangesForNotification
ininit
is a good practice to ensure notification always register in main thread? which i read from this answer