Posting NSNotification on the main thread

IosObjective CCocoaNsnotificationcenter

Ios Problem Overview


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];
});

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

Yes

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

Solution 3 - Ios

YES

Swift 2 syntax

dispatch_async(dispatch_get_main_queue()) {
    NSNotificationCenter.defaultCenter().postNotificationName("updateSpinner", object: nil, userInfo: ["percent":15])
}

Swift 3 syntax

DispatchQueue.main.async {
    NotificationCenter.default.post(name: "updateSpinner", object: nil, userInfo: ["percent":15])
}

Solution 4 - Ios

Somewhere along the line this became possible with:

addObserver(forName:object:queue:using:)

which is [here][1], but the whole point is the queue object.

> The operation queue to which block should be added. If you pass nil, > the block is run synchronously on the posting thread.

So how do you get the queue that corresponds to the main runloop?

let mainQueue = OperationQueue.main

Note: this is when you are subscribing to notifications, so you do it once and you're done. Doing it on every single call is terribly redundant. [1]: https://developer.apple.com/reference/foundation/notificationcenter/1411723-addobserver

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRunLoopView Question on Stackoverflow
Solution 1 - IosAnoop VaidyaView Answer on Stackoverflow
Solution 2 - IosUndoView Answer on Stackoverflow
Solution 3 - IosWhipsterCZView Answer on Stackoverflow
Solution 4 - IosDan RosenstarkView Answer on Stackoverflow