Inter-Process Communication on Mac – Know How part4(Distributed Notifications)
In Part 1 of this blog series we discussed about various techniques available for Inter-Process Communication (IPC) on Mac OS X.
As discussed in previous blog Distributed notifications work as follows :
1. A notification center manages the sending and receiving of notifications. It notifies all observers of notifications meeting specific criteria.
2. The notification information is encapsulated in NSNotification objects. Client objects register themselves with the notification center as observers of specific notifications posted by other objects.
3. When an event occurs, an object posts an appropriate notification to the notification center.
Posting a distributed notification is an expensive operation. The notification gets sent to a system wide server that then distributes it to all the processes that have objects registered for distributed notifications. The latency between posting the notification and the notification’s arrival in another process is unbounded. In fact, if too many notifications are being posted and the server’s queue fills up, notifications can be dropped.
Let’s look at a simple code to send and receive distributed notification.
Common to Server and Client
#define NotificationName @”Notification from ServerApp”
#define NotificationObject @”com.testnotif.com”
Create a action to send a dictionary in a distributed notification.
NSDictionary* itemTobeSent = [NSDictionary dictionaryWithObjectsAndKeys:@”Notification sent”,@”key”, nil];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:NotificationName object:NotificationObject userInfo:itemTobeSent options:NSNotificationDeliverImmediately];
First register for the distributed notification as follows :
– (void)applicationDidFinishLaunching:(NSNotification *)aNotification
// Insert code here to initialize your application
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification: ) name:NotificationName object:NotificationObject];
Write implementation of receivedNotification function
NSLog(@”recieved notification : %@”,[notification.userInfo objectForKey:@”key”]);
//Do something with received dictionary.
For more info and detailed documentation see Notification Programming.
Posted on November 11, 2014, in Cocoa Application, Cooca, Inter-Process Communication (IPC), Objective-C and tagged Distributed Notification, Hem Dutt, IPC, MAC OSX, Mindfire Solutions, notification, Notification Center, NSNotificationDeliverImmediately, Objective-C, Xcode. Bookmark the permalink. 2 Comments.