1

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.

1

1 Answer 1

0

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
1
  • Thank you for your help. But I came across a question which is more difficult : When I did not set the MainPageViewController as Initial VC instead of using a segue to call it. applicationDidFinishLaunching => call setter => setter posts notification ≠> print context , which means debugLog(@"context = %@", self.managedObjectContext) did not run, and MainPageCDTVC did not obtain the context. WHY?
    – wl song
    Apr 2, 2015 at 8:50

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.