Search code examples
iosmultithreadingnsnotificationcenter

Multithreading and NSNotification in iOS


I wrote a few functions in AppDelegate.m like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    debugMethod();
    self.WeiboStatusesDatabaseContext = [self createMainQueueManagedObjectContext];
    return YES;
}

- (void)setWeiboStatusesDatabaseContext:(NSManagedObjectContext *)WeiboStatusesDatabaseContext
{
    debugMethod();
    _WeiboStatusesDatabaseContext = WeiboStatusesDatabaseContext;
    NSDictionary *userInfo = self.WeiboStatusesDatabaseContext ? @{WeiboSTATUSESDatabaseAvailabilityContext : self.WeiboStatusesDatabaseContext} : nil;
    [[NSNotificationCenter defaultCenter] postNotificationName:WeiboSTATUSESDatabaseAvailabilityNotification
                                                    object:self
                                                  userInfo:userInfo];
}

And I also wrote a function in my MainPageCDTVC.m which is a CoreData TableViewController :

- (void)awakeFromNib
{
    debugMethod();
    [[NSNotificationCenter defaultCenter] addObserverForName:WeiboSTATUSESDatabaseAvailabilityNotification
                                                  object:nil
                                                   queue:nil
                                              usingBlock:^(NSNotification *note) {
                                                  self.managedObjectContext = note.userInfo[WeiboSTATUSESDatabaseAvailabilityContext];
                                                  debugLog(@"context = %@", self.managedObjectContext);
                                              }];
    debugLog(@"Context = %@", self.managedObjectContext);
}  

When I set MainPageCDTVC as Initial ViewController and run this app, I got a result from Xcode console :

2015-04-02 11:37:38.650 WeiboDemo[962:48379] -[MainPageCDTVC awakeFromNib]
2015-04-02 11:37:38.665 WeiboDemo[962:48379] Context = (null)
2015-04-02 11:37:38.669 WeiboDemo[962:48379] -[AppDelegateapplication:didFinishLaunchingWithOptions:]
2015-04-02 11:37:38.673 WeiboDemo[962:48379] file:///Users/wlSong/Library/Developer/CoreSimulator/Devices/7DCCFF56-7A91-4DCC-BB20-CE33FCAE42BF/data/Containers/Data/Application/82C32B6F-A842-4BFD-A9DF-0126A7C290BC/Documents/
2015-04-02 11:37:38.691 WeiboDemo[962:48379] managedObjectContext have created.
2015-04-02 11:37:38.691 WeiboDemo[962:48379] -[AppDelegate setWeiboStatusesDatabaseContext:]
2015-04-02 11:37:38.695 WeiboDemo[962:48379] context = < NSManagedObjectContext: 0x7b84da00 >

My questions are:

  • Why did "context = < NSManagedObjectContext: 0x7b84da00 >" appear in the last?
  • Are that -[AppDelegate application:didFinishLaunchingWithOptions:] and -[MainPageCDTVC awakeFromNib] in the same thread?
  • If they are who run first?

PS: I am a Chinese, my English is poor. Forgive me.


Solution

  • The order is:

    1. awakeFromNib
    2. applicationWillFinishLaunching
    3. applicationDidFinishLaunching

    You don't have to worry about multi-threading/parallelism here. If you follow the order you will see:

    1. awakeFromNib => defaultCenter observer add => print (null) context
    2. applicationDidFinishLaunching => call setter => setter posts notification => print context