NSNotificationを使って通知をメインスレッドから投げる

NSNotificationの通知は、NSNotificationCenter の postNotification メソッドを呼んだスレッドで実行される。
一方、iOSアプリではUI操作はメインスレッドからでないと実行できない。
よって、通知を受け取ってUI操作をしたいという場合は以下のようにしてメインスレッドから通知を投げるようにすればよい。


NSNotification* n = [NSNotification notificationWithName:notificationName object:userInfo];
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc performSelector:@selector(postNotification:) onThread:[NSThread mainThread] withObject:n waitUntilDone:NO];