ios NSNotificationCenter 收到通知后的执行线程

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

Delivering Notifications To Particular Threads

Regular notification centers deliver notifications on the thread in which the notification was posted. Distributed notification centers deliver notifications on the main thread. At times, you may require notifications to be delivered on a particular thread that is determined by you instead of the notification center. For example, if an object running in a background thread is listening for notifications from the user interface, such as a window closing, you would like to receive the notifications in the background thread instead of the main thread. In these cases, you must capture the notifications as they are delivered on the default thread and redirect them to the appropriate thread.

One way to redirect notifications is to use a custom notification queue (not an NSNotificationQueue object) to hold any notifications that are received on incorrect threads and then process them on the correct thread. This technique works as follows. You register for a notification normally. When a notification arrives, you test whether the current thread is the thread that should handle the notification. If it is the wrong thread, you store the notification in a queue and then send a signal to the correct thread, indicating that a notification needs processing. The other thread receives the signal, removes the notification from the queue, and processes the notification.

时间: 2024-08-09 23:52:04

ios NSNotificationCenter 收到通知后的执行线程的相关文章

IOS中的通知NSNotification

类似于Android中的广播接收者发送者 1.一般完整的通知包含三个属性 1 -(NSString *)name ;//通知的名称 2 -(id)object ;//通知发布者(是谁要发布通知) 3 -(NSDictionary *)userInfo;//一些额外的信息(通知发布者传递给通知接收者的信息内容) 2.初始化一个通知(NSnotification)对象 1 +(instancetype)notificationWithName:(NSString *)aName object:(id

Swift - 使用NSNotificationCenter发送通知,接收通知

1,通知(NSNotification)介绍 这里所说的通知不是指发给用户看的通知消息,而是系统内部进行消息传递的通知.要介绍通知之前,我们需要先了解什么是观察者模式. 观察者模式 (Observer):指一个对象在状态变化的时候会通知另一个对象.参与者并不需要知道其他对象的具体是干什么的 .这是一种降低耦合度的设计.常见的使用方法是观察者注册监听,然后在状态改变的时候,所有观察者们都会收到通知. 在 MVC 里,观察者模式意味着需要允许 Model 对象和 View 对象进行交流,而不能有直接

iOS开发之通知使用总结

通知中心(NSNotificationCenter) 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发布通知(NSNotification),描述自己在做什么.其他感兴趣的对象(Observer)可以申请在某个特定通知发布时(或在某个特定的对象发布通知时)收到这个通知 通知(NSNotification) 一个完整的通知一般包含3个属性: - (NSString *)name; // 通知的名称 -

iOS中的通知机制

每一个应用程序都有一个通知中心,NSNotificationCenter,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发布通知NSNotification,描述自己在做什么.其他感兴趣的对象(Observer)可以申请在某个特定通知发布时(或在某个特定的对象发布通知时)收到这个通知 ?一个完整的通知一般包含3个属性: ?- (NSString *)name; // 通知的名称 ?- (id)object; // 通知发布者(是谁要发布通知) ?- (NSDictionary

iOS开发之通知机制

1.通知中心 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发布通知(NSNotification),描述自己在做什么.其他感兴趣的对象(Observer)可以申请在某个特定通知发布时(或在某个特定的对象发布通知时)收到这个通知 通知与代理的区别:通知是多对多的(类似android中的广播),代理是一对一的. 比如,键盘弹出的时候也会发送一条通知. 2.通知(NSNotification) 一个完整

iOS开发之:NSNotificationCenter(通知)的使用方法

iOS软件开发的时候会遇到这种情况:打开APP后会在后台运行某个方法,例如下载文件,下载完成后可能需要调用某个方法来刷新界面,这时候可能没法在下载的函数中回调.NSNotificationCenter(通知)是一个很好的选择. 通知使用起来非常的简单: 1. 定义将要调用的方法: - (void)callBack{     NSLog(@"thisis Notification."); } 2. 定义通知: [[NSNotificationCenter defaultCenter] a

C# 多线程join的用法,等待多个子线程结束后再执行主线程

等待多个子线程结束后再执行主线程 class MultiThread{ #region join test public void MultiThreadTest() { Thread[] ths = new Thread[2]; ths[0] = new Thread(Method1); ths[1] = new Thread(Method2); foreach (Thread item in ths) { //首先让所有线程都启动 item.Start(); //试想一下在这里加上item.

iOS开发-消息通知机制(NSNotification和NSNotificationCenter)

iOS中委托模式和消息机制基本上开发中用到的比较多,一般最开始页面传值通过委托实现的比较多,类之间的传值用到的比较多,不过委托相对来说只能是一对一,比如说页面A跳转到页面B,页面的B的值改变要映射到页面A,页面C的值改变也需要映射到页面A,那么就需要需要两个委托解决问题.NSNotificaiton则是一对多注册一个通知,之后回调很容易解决以上的问题. 基础概念 iOS消息通知机制算是同步的,观察者只要向消息中心注册, 即可接受其他对象发送来的消息,消息发送者和消息接受者两者可以互相一无所知,完

IOS NSNotificationCenter 通知的使用

1.注册通知 [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notify) name:@"myNotify" object:nil]; -(void)notify { NSLog(@"notify"); } 2.触发通知 [NSNotificationCenter defaultCenter] postNotificationName:@"myNotify&