LoginSignup
12
11

More than 5 years have passed since last update.

NSNotificationCenterの通知でMainThread実行

Posted at

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更新という利用シーンは多いはずなので、カテゴリあたりでラップしてあげるともっと使いやすくなりそう。

12
11
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11