我在AppDelegate.m中编写了一些函数,如下所示:
- (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];
}
我还在MainPageCDTVC.m中编写了一个函数,它是一个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);
}
当我将MainPageCDTVC设置为初始ViewController并运行此应用程序时,我从Xcode控制台获得了一个结果:
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 >
我的问题是:
答案 0 :(得分:0)
订单是:
awakeFromNib
applicationWillFinishLaunching
applicationDidFinishLaunching
您不必担心多线程/并行性。如果您按照订单操作,您将看到:
awakeFromNib
=&gt; defaultCenter observer add =&gt; print(null)context applicationDidFinishLaunching
=&gt; call setter =&gt; setter posts notification =&gt;打印背景