iOS 中的多线程和 NSNotification
Multithreading and NSNotification in iOS
我在 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 表ViewController :
- (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 设置为 Initial 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 >
我的问题是:
- 为什么最后出现"context = < NSManagedObjectContext: 0x7b84da00 >"?
- -[AppDelegate application:didFinishLaunchingWithOptions:] 和 -[MainPageCDTVC awakeFromNib] 是否在同一个线程中?
- 如果他们是谁 运行 第一?
PS: 我是中国人,英语很差。见谅。
顺序是:
awakeFromNib
applicationWillFinishLaunching
applicationDidFinishLaunching
这里不用担心multi-threading/parallelism。如果您按照顺序操作,您将看到:
awakeFromNib
=> defaultCenter 观察者添加 => 打印(空)上下文
applicationDidFinishLaunching
=> 调用 setter => setter 发布通知 => 打印上下文
我在 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 表ViewController :
- (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 设置为 Initial 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 >
我的问题是:
- 为什么最后出现"context = < NSManagedObjectContext: 0x7b84da00 >"?
- -[AppDelegate application:didFinishLaunchingWithOptions:] 和 -[MainPageCDTVC awakeFromNib] 是否在同一个线程中?
- 如果他们是谁 运行 第一?
PS: 我是中国人,英语很差。见谅。
顺序是:
awakeFromNib
applicationWillFinishLaunching
applicationDidFinishLaunching
这里不用担心multi-threading/parallelism。如果您按照顺序操作,您将看到:
awakeFromNib
=> defaultCenter 观察者添加 => 打印(空)上下文applicationDidFinishLaunching
=> 调用 setter => setter 发布通知 => 打印上下文