Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting NSNotification on the main thread

I found the following code snippet which allows NSNotification to be posted on the main thread from any background thread. I would like to know if this is a safe and acceptable practice please?

dispatch_async(dispatch_get_main_queue(),^{     [[NSNotificationCenter defaultCenter] postNotificationName:@"ImageRetrieved"                                                          object:nil                                                        userInfo:imageDict]; }); 
like image 322
RunLoop Avatar asked Apr 04 '13 14:04

RunLoop


2 Answers

Yes you can.

Generally you want the NSNotifications to be sent on the main , especially if they trigger UI activities like dismissing a modal login dialog.

Delivering Notifications To Particular Threads

Regular notification centers deliver notifications on the thread in which the notification was posted. Distributed notification centers deliver notifications on the main thread. At times, you may require notifications to be delivered on a particular thread that is determined by you instead of the notification center. For example, if an object running in a background thread is listening for notifications from the user interface, such as a window closing, you would like to receive the notifications in the background thread instead of the main thread. In these cases, you must capture the notifications as they are delivered on the default thread and redirect them to the appropriate thread.

like image 83
Anoop Vaidya Avatar answered Oct 14 '22 08:10

Anoop Vaidya


Yes

This is - you are getting into the main thread and posting your notification. Can't get any safer than that.

like image 40
Undo Avatar answered Oct 14 '22 06:10

Undo