[英]How can I send a userInfo dict using CFNotificationCenterGetDarwinNotifyCenter()
I need to send an object using CFNotificationCenterGetDarwinNotifyCenter()
however, I'm noticing that whenever I send a notification using 我需要使用
CFNotificationCenterGetDarwinNotifyCenter()
发送一个对象,但是,每当我使用发送通知时,我都会注意到
const void *tkeys[1] = { @"testKey" };
const void *tvalues[1] = { @"testValue" };
CFDictionaryRef userInfo = CFDictionaryCreate(NULL, tkeys, tvalues, 1, NULL, NULL);
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
CFSTR("foo"),
object,
userInfo,
true);
then I find that in the observer callback, the userInfo dict is NULL. 然后我发现在观察者回调中,userInfo dict为NULL。 Is there something I am missing here?
这里有什么我想念的吗? Is there some other way by which I can possibly send the userInfo object?
是否有其他方式可以发送userInfo对象? I was looking at http://nshipster.com/inter-process-communication/ and the Distributed Notifications part, however whenever I use :
我正在查看http://nshipster.com/inter-process-communication/和分布式通知部分,但每当我使用时:
CFNotificationCenterRef distributedCenter =
CFNotificationCenterGetDistributedCenter();
then I get an error Invalid Use of Function
error whenever I try it. 然后
Invalid Use of Function
每当我尝试它时,我都会收到错误Invalid Use of Function
错误。
If you read the documentation for CFNotificationCenterGetDarwinNotifyCenter
, you'll find this caveat: 如果您阅读了
CFNotificationCenterGetDarwinNotifyCenter
的文档 ,您会发现以下警告:
Several function parameters are ignored by Darwin notification centers.
Darwin通知中心忽略了几个功能参数。 To ensure future compatibility, you should pass NULL or 0 for all ignored arguments.
为确保将来的兼容性,您应该为所有被忽略的参数传递NULL或0。
userInfo
is one of those parameters. userInfo
是其中一个参数。 CFNotificationCenter.h
goes into more detail: CFNotificationCenter.h
详细介绍:
// For this center, there are limitations in the API. There are no notification "objects",
// "userInfo" cannot be passed in the notification, ...
// - CFNotificationCenterPostNotification(): the 'object', 'userInfo', and 'deliverImmediately' arguments are ignored.
Why? 为什么? Dig a little deeper.
深入挖掘一下。 The documentation also mentions that this type of notification center is built on the Core OS notify mechanism declared in
notify.h
. 该文档还提到这种类型的通知中心是基于
notify.h
声明的Core OS通知机制notify.h
。 If you look that up, you'll find that there is only one way to post a notification, by calling notify_post()
, which takes only a single string argument. 如果你仔细观察,你会发现只有一种方法可以发布通知,方法是调用
notify_post()
,它只需要一个字符串参数。 There is no way to add additional data to the notification. 无法向通知添加其他数据。 This is a very old and rudimentary IPC mechanism that doesn't know anything about CoreFoundation data structures.
这是一个非常古老而基本的IPC机制,它对CoreFoundation数据结构一无所知。
As for CFNotificationCenterGetDistributedCenter
, that isn't available on iOS. 至于
CFNotificationCenterGetDistributedCenter
,这在iOS上不可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.