If you want to simply share a message that a job has completed and perhaps have several other classes listening out then NSNotificationCenter might be the best option for you.
Note: Using NSNotificationCenter we can send the data only with in the app. we can not send data to other apps.
Notifications classes
Foundation provides 3 classes to deal with all your notification needs:
- NSNotification: represents a notification.
- NSNotificationCenter: broadcasts notifications and manages observers. Each app has a default notification center via defaultCenter.
- NSNotificationQueue: coalesces and delays notifications. Each thread has a default notification queue via defaultQueue.
Receiving notifications: To receive a notification you need to know only its name.
[NSNotificationCenter DefaultCenter]addObserver: self selector:@selector(applicationDidReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
Observer: Object that receive a notification is called observer.Observers register to a specific notification by specifying the name of the notification (usually a constant) and the sender of the notification as filters. Both are optional; in the above example, we don’t care who the sender is so we pass
nil
.
Handling Notification:
Observers will receive notifications until they are removed from the
Removing Observer: NSNotificationCenter provides two methods to remove observer.
Posting the Notification:Each app has a default NSNotificationCenter instance that can be used to broadcast notifications. You can create the notification object yourself and post it, or use a couple of convenience methods that do this. If you don’t need to send any additional information, posting a notification is as simple as:
[[NSNotificationCenter DefaultCenter] postNotificationName:notificationName object:notificationsender];
The notification is then sent to every registered observer in no defined order. Notifications are posted synchronously, meaning that postNotification: and its convenience methods will not return until all observers have processed the notification. Additionally, observers will be called in the same thread in which the notification is posted. Unless you explicitly state otherwise, observers will expect this to be the main thread.
NSNotificationCenter
.Each notification is sent once per addObserver call.NSNotificationCenter will call the selector provided in addObserver:selector:name:object:
with a single argument of type NSNotification
. The selector is called in the same thread in which the notification was posted.Removing Observer: NSNotificationCenter provides two methods to remove observer.
removeObserver:
and removeObserver:name:object:
. The former unregisters the observer for all notifications, and the latter does so for notifications of the given name and sender, either of which can be nil
.Posting the Notification:Each app has a default NSNotificationCenter instance that can be used to broadcast notifications. You can create the notification object yourself and post it, or use a couple of convenience methods that do this. If you don’t need to send any additional information, posting a notification is as simple as:
[[NSNotificationCenter DefaultCenter] postNotificationName:notificationName object:notificationsender];
The notification is then sent to every registered observer in no defined order. Notifications are posted synchronously, meaning that postNotification: and its convenience methods will not return until all observers have processed the notification. Additionally, observers will be called in the same thread in which the notification is posted. Unless you explicitly state otherwise, observers will expect this to be the main thread.
You can send additional information also as:
NSDictionary *userInfo = @{@"myKey",@"myValue"};
[[NSNotificationCenter DefaultCenter] postNotificationName:notificationName object:notificationsender];
No comments:
Post a Comment