24/7 twenty-four seven

iOS/OS X application programing topics.

メインスレッド以外のスレッドから NSNotification で通知する場合

NSNotification の通知は、別スレッドから呼ぶと、そのスレッドで実行されるので、通知を受けて UI を更新するような場合、都合が悪いことがあります。
(UIKit は Thread Safe でないため)


そういうときは、下記のようにメインスレッドから通知するといいと思います。

NSNotification *notification = [NSNotification notificationWithName:NOTIFICATION object:self];
[notificationCenter performSelector:@selector(postNotification:) 
                           onThread:[NSThread mainThread]
                         withObject:notification
                      waitUntilDone:NO];