0

I am using NSDistributedNotifications as an IPC on macOS between two processes. Sometimes I see that the notification is sent but is not received and the desired operation is not executed.

Sender Code:

[[NSDistributedNotificationCenter defaultCenter]postNotificationName:@"doSomething" object:myObject userInfo:config deliverImmediately:YES];

Receiver's code:

[[NSDistributedNotificationCenter defaultCenter]addObserver:self selector:@selector(myfunc:) name:@"doSomething" object:myObject];

Is there a way to debug this? Can I run some command line utility like dtrace or so to see if the notification was even sent to the desired process? Any help will be appreciable

ZestyZest
  • 911
  • 13
  • 27
  • Where does the receiver's `myObject` come from? – Willeke Oct 19 '16 at 00:28
  • it is a common string between sender and receiver that I use to uniquely identify the sender. – ZestyZest Oct 19 '16 at 00:39
  • Is it the same object (same address)? – Willeke Oct 19 '16 at 14:43
  • It is a string and the sender and receiver dont share the same object. Think of it as a key that identifies the sender which essentially defines "I am interested in notifications only sent by the process with the key". If I set it to `nil`, the process will receive all distributed notifications. – ZestyZest Oct 19 '16 at 20:40
  • You're observing the notificiations sent by the receiver's `myObject`. – Willeke Oct 20 '16 at 14:39
  • Yes .. sender's object = receiver's object – ZestyZest Oct 20 '16 at 18:04
  • No, sender's object is equal to receiver's object. They aren't the same object. – Willeke Oct 21 '16 at 16:01
  • They don't have the same address in memory. But they do have the same string value which I have denoted by "myObject". Note that I do receive notifications 75% of the time but for some unknown reasons, I don't receive it at random time. – ZestyZest Oct 21 '16 at 17:59
  • Maybe this question explains why it works: [Why do two NSStrings taken from user input end up with the same address?](http://stackoverflow.com/questions/35801842/why-do-two-nsstrings-taken-from-user-input-end-up-with-the-same-address/35808455#35808455) – Willeke Oct 21 '16 at 19:26
  • I get your point but I think whatever I have read on [Apple's doc](https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/PreferencePanes/Tasks/Communication.html) says different. `When using distributed notifications, though, the object must be a string (CFString or NSString). A useful choice for the observed string is the bundle identifier of the target application.` Can you confirm if the object should really have the same address? – ZestyZest Oct 22 '16 at 06:03
  • I didn't read the whole story, only the documentation of `addObserver`. What you're saying makes more sense. – Willeke Oct 22 '16 at 13:57

0 Answers0