kvo nsnotificationncenter 线程安全吗

&&国之画&&&& &&
版权所有 京ICP备号-2
迷上了代码!NSNotificationCenter 的使用详解 - 扬名 - 博客园
posts - 139, comments - 79, trackbacks - 0, articles - 0
- (void)execute:(NSNotification *)notification {
//do something when received notification
//notification.name is @"NOTIFICATION_NAME"
if(notification.object && [notification.object isKindOfClass:[Test class]]){
//do something
1 [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(execute:)
name:@"NOTIFICATION_NAME"
object:nil];
NOTIFICATION_NAME"
object:nil];
//或者用下面几行代码,明确的 notification 示例
Test *test = [[Test alloc] init];
NSNotification *notification = [NSNotification notificationWithName:@"NOTIFICATION_NAME"
object:test];
[[NSNotificationCenter defaultCenter] postNotification:notification];
NOTIFICATION_NAME"
object:test];//object 与注册时相同
//或[[NSNotificationCenter defaultCenter] removeObserver:self];
object:nil];In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself.
这句话是说,notification都是在post时的线程中被传递和接收,即使add observer时是 在另外一个线程中。
例如: A, B两个线程都addObserver。如果A中post,则无论此时B线程是否退出,回调都 在A中触发,且触发2次。
来源:http://blog.csdn.net/lwl_ls/archive//6336083.aspx
声明:该文章系网友上传分享,此内容仅代表网友个人经验或观点,不代表本网站立场和观点;若未进行原创声明,则表明该文章系转载自互联网;若该文章内容涉嫌侵权,请及时向
上一篇:下一篇:
相关经验教程
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
论文写作技巧NSNotificationCenter&的使用-iPhone成长之路
iPhone中NSNotificationCenter,的使用很简单。
我们下面就看一看,通知的使用流程
1.某个页面,发布了通知,告诉应用程序,通知的内容,及接到响应后,做出的操作。
// 注册通知-播放页面
& & [[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(PlayVideo:)
& name:@"PlayVideo"
object:nil];
addObserver:self页面发布了名称为name:@"PlayVideo"的通知,当收到响应后,将执行selector:@selector(PlayVideo:)方法。
我们在另一个页面(本页面也是可以的,不过似乎没有必要啊,毕竟同一个页面可以相互访问,没必要再通过“通知”了)中,符合了某种条件,于是,该页面发送一个“通知“来告知"发送通知的主人"。这样,通知的主人,将开始执行响应的操作(PlayVideo:)。
2.发送通知来告知通知的主人
// 发送通知
& & NSNotification *notification=
[NSNotification notificationWithName:@"PlayVideo"
object:showViewController userInfo:dict];
& & [[NSNotificationCenter
defaultCenter] postNotification:notification];
3.发送通知,后,通知的主人,得到了响应,于是开始执行相关方法:
-(void) PlayVideo:(NSNotification *)notification {
& & if(notification.object
&& [notification.object
isKindOfClass:[ShowViewController class]]){
ShowViewController *view=(ShowViewController *)[notification
NSDictionary *userInfo= [notification userInfo];
UINavigationController* navController =
(UINavigationController*)self.tabBarController.selectedViewC
(navController!=Nil && view!=Nil)
& & [navController
pushViewController:view animated:TRUE];
还有,忘记一个重要的东西,现在来补充一下。
注册&取消通知
当我们注册通知后,当不在需要使用时,要记得取消该通知。
比如,我们在viewDidLoad方法中,注册通知
- (void)viewDidLoad
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(controllSearchBarType)
& & name:@"controllSearchBarType"
& object:nil];
在viewDidUnload方法中,取消通知
- (void)viewDidUnload{
[[NSNotificationCenter
defaultCenter]removeObserver:self
name:@"controllSearchBarType"
& & object:nil];
通知传递的参数有2个:
1.[notification&object]
2.[notification&userInfo]
就是这样,简单把,这就是通知。
通知主要用在:
1.不同的类,页面中传递信息。
2.在一些特定事件中,进行通知,也许通知并不执行太多操作,仅仅是作为一个该执行操作的"型号”。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 nsnotification 的文章

 

随机推荐