zoukankan      html  css  js  c++  java
  • NSNotification的几点说明

    1、NSNotification消息的同步性

    ①NSNotification使用的是同步操作。即如果你在程序中的A位置post了一个NSNotification,在B位置注册了一个observer,通知发出后,必须等到B位置的通知回调执行完以后才能返回到A处继续往下执行。

    因此,不要过多的或者低效的使用NSNotification,《Cocoa基本原理指南》一文推荐的方式是通过一些“中间的”观察者将通告的结果传递给它们可以访问的对象。

    ②如果想让NSNotification的post处和observer处异步执行,可以通过NSNotificationQueue实现。

    2、多个观察者的执行顺序

    对于同一个通知,如果注册了多个观察者,则这多个观察者的执行顺序和他们的注册顺序是保持一致的。

    3、NSNotification通知转发线程

    ①NSNotificationCenter在转发NSNotification消息的时候,在哪个线程中post,就在哪个线程中转发。换句话说,不管你的observer是在哪个线程,observer的回调方法执行线程都和post的线程保持一致。

    ②如果想让post的线程和转发的线程不同,可以通过NSNotification重定向技术实现。

    4、addObserver和removeObserver必须成对出现

    官方文档中是这样描述的:

    The notification center does not retain its observers, therefore, you must ensure that you unregister observers (usingremoveObserver: or removeObserver:name:object:) before they are deallocated. (If you don’t, you will generate a runtime error if the center sends a message to a freed object.)

    再addObserver的时候,notification center并不增加观察者对象的引用计数,因此,在观察者对象被释放之前我们必须保证它们被从观察队列中移除,否则后果很明显!

    官方文档:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Notifications/Articles/Threading.html#//apple_ref/doc/uid/20001289-CEGJFDFG

    来源:http://blog.csdn.net/wzzvictory/article/details/8489516

  • 相关阅读:
    Tornado输出和响应头
    sqlalchemy 学习(二)scoped session
    04:sqlalchemy操作数据库 不错
    sqlalchemy(二)高级用法
    红黑树
    Minimum Window Substring
    Max Points on a Line
    分治算法
    Maximum Subarray
    Word Break
  • 原文地址:https://www.cnblogs.com/benbenzhu/p/3491023.html
Copyright © 2011-2022 走看看