Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it ok to use of a notification to communication back to the main thread of an IOS app? (cf performSelectorOnMainThread)

Is it ok to use of a notification to communication back to the main thread of an IOS app? (cf performSelectorOnMainThread). That is, there are are there any gottcha's for this purpose?

Background

  • want to call back to main UI thread from a background thread (e.g. performSelectorInBackground)
  • could use performSelectorOnMainThread to communicate back, but wondering if it is OK to use a notification?

For example

 [[NSNotificationCenter defaultCenter] postNotificationName:@"ModelChanged" object:self];
like image 453
Greg Avatar asked Nov 07 '11 04:11

Greg


People also ask

What is communication notification?

Communication notifications are unique in their ability to break through the scheduled notification summary by default, and can also break through a Focus. Enable the Communication Notifications capability in your app and include the activity types that your app supports in its NSUserActivityTypes array.

What is notification service extension?

A notification service app extension doesn't present any UI of its own. Instead, it's launched on demand when the system delivers a notification of the appropriate type to the user's device. You use this extension to modify the notification's content or download content related to the extension.


1 Answers

Actually there is a gottcha; you'll crash randomly! That has been my experience. This has to do with the fact that the object receiving the notification does so on the same thread as the notification's sender.

From the Apple iOS Documentation on Notification Centers:

In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself.

This will inevitably cause you headaches.

If the notification is being received by something on the main thread, I've found that popping into the main thread from the background thread to issue a notification is the safest way to go about this. It is quite simple to do:

//Call this to post a notification and are on a background thread      
- (void) postmyNotification{
  [self performSelectorOnMainThread:@selector(helperMethod:) withObject:Nil waitUntilDone:NO];
}

//Do not call this directly if you are running on a background thread.
- (void) helperMethod{
  [[NSNotificationCenter defaultCenter] postNotificationName:@"SOMENAME" object:self];
}

Unfortunately this introduces a subtle coupling between the sender and receiver in that you are modifying the sender to accommodate the receiver.

An even better solution, as XJones points out, is to have the sender send out the notification on whatever thread it decides to, and then to make the listener responsible for using the proper thread to perform whatever action it needs to.

Hope that was helpful.

like image 70
PixelCloudSt Avatar answered Sep 24 '22 13:09

PixelCloudSt