what is the CFNotificationCenter callback function observer?

喜欢而已 提交于 2019-12-13 01:29:49

问题


I'm new to the CFNotificationCenter. I was reading the documentation at developer.apple and it says that in the CallbackFunction that observer is "an arbitrary value other than NULL that identifies the observer". Can the observer be an object so that I can access its methods inside the callback function? These are the parameters as shown in the documentation.

   void MyCallBack (
       CFNotificationCenterRef center,
       void *observer,
       CFStringRef name,
       const void *object,
       CFDictionaryRef userInfo
    );

Another question. Why is it allowed for an observer to be nul when adding an observer??

void CFNotificationCenterAddObserver (
   CFNotificationCenterRef center,
   const void *observer,
   CFNotificationCallback callBack,
   CFStringRef name,
   const void *object,
   CFNotificationSuspensionBehavior suspensionBehavior
);

"The observer. In OS X v10.3 and later, this parameter may be NULL."


回答1:


The observer argument is an arbitrary pointer that you pass to CFNotificationCenterAddObserver, which it stores along with your callback pointer. When the notification center calls your callback, it passes the observer pointer as an argument to the callback.

That's it. The notification center doesn't dereference the observer pointer, or try to retain it. It's a way for you to pass extra info to your callback if you need to. If you want to use it to reference an Objective-C object, you can. You should make sure the object won't be deallocated while the observer is registered.



来源:https://stackoverflow.com/questions/11838753/what-is-the-cfnotificationcenter-callback-function-observer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!