Notificationを使ってUIの更新をしたい場合、NSNotificationCenter
からperformSelectorOnMainThread
を呼んであげると良い。##例Objective-C NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center performSelectorOnMainThread:@selector(postNotification:) withObject:[NSNotification notificationWithName:@"name" object:nil] waitUntilDone:NO];
期待通りUIが更新される。##良くない例Objective-C NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center postNotification:@"name"];
通常の呼び方だと新スレッドでメソッドが実行されるようなので、これだとUIがうまく更新されない。##利用するシーン自分の場合は、
C言語のライブラリを使っていて、「C言語のcallbackからどうやってUI更新するんだ?」と悩んでいたところ、これで解決。
NotificationからUI更新という利用シーンは多いはずなので、カテゴリあたりでラップしてあげるともっと使いやすくなりそう。