警告iOS / iPhone用户有关重复的NSNotification观察 [英] Warning for iOS/iPhone users about duplicate NSNotification observations

查看:126
本文介绍了警告iOS / iPhone用户有关重复的NSNotification观察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是一个问题,而是警告其他人保存一段时间。

This isn't a question so much as a warning to others to save them some time.

iOS 3 / iPhone OS 3上的NSNotificationCenter(我假设Mac OS X和iOS 4)也有以下行为:

NSNotificationCenter on iOS 3/iPhone OS 3 (I'm assuming also Mac OS X and iOS 4) has the following behavior:

如果您为确切的特定通知多次注册,NSNotificationCenter将 NOT 识别冗余,而不是在你注册观察时向你发出尽可能多的通知。

If you register yourself multiple times for the exact specific notification, NSNotificationCenter will NOT recognize the redundancy and instead will fire off as many notifications to you as you've registered an observation for.

这几乎不是你想要看到的行为,几乎是总是意外。

This is almost never the behavior you want to see and is almost always accidental.

示例:

我希望我的视图控制器在新的时候从单个网络对象接收通知数据来自:

I want my view controller to receive notifications from a singleton network object when new data comes in:

- (void) viewDidLoad 
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(newDataArrived:) 
                name:NewDataArrivedNotification
              object:[NetworkListener sharedNetworkListener]];
}

但早些时候我已经把相同的东西放在 viewWillAppear

but earlier I'd already put the same thing in viewWillAppear:

- (void) viewWillAppear
{
    [super viewWillAppear];

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(newDataArrived:)
                name:NewDataArrivedNotification
              object:[NetworkListener sharedNetworkListener]];
}

请注意,完全相同的通知,解决同一个观察者,发件人和通知名称。

Note that it's exactly the same notification, resolving to the same observer, sender and notification name.

在这种情况下,如果我不删除其中一个addObserver调用,我将收到重复的通知给我的视图控制器。

In this case, if I don't remove one of those addObserver calls, I'll get duplicate notifications to my view controller.

在多线程环境中,这是一个受伤的世界。相信我。

In a multi-threaded environment, this is a world of hurt. Trust me.

只要有其他人遇到这样的事情,就把它放在那里。

Just putting this out there in case there are others who run into something like this.

推荐答案

iOS 3 / iPhone OS 3上的NSNotificationCenter(我也假设Mac OS X和iOS 4)具有以下行为:

NSNotificationCenter on iOS 3/iPhone OS 3 (I'm assuming also Mac OS X and iOS 4) has the following behavior:

如果您为确切的特定通知多次注册,NSNotificationCenter将识别冗余,而是在您注册观察时向您发出尽可能多的通知。

If you register yourself multiple times for the exact specific notification, NSNotificationCenter will NOT recognize the redundancy and instead will fire off as many notifications to you as you've registered an observation for.

这几乎不是你想要看到的行为,几乎总是偶然的。

This is almost never the behavior you want to see and is almost always accidental.

示例:

我希望我的视图控制器在新数据进入时从单个网络对象接收通知:

I want my view controller to receive notifications from a singleton network object when new data comes in:

- (void) viewDidLoad 
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(newDataArrived:) 
                name:NewDataArrivedNotification
              object:[NetworkListener sharedNetworkListener]];
}

但早些时候我已经把相同的东西放在 viewWillAppear

but earlier I'd already put the same thing in viewWillAppear:

- (void) viewWillAppear
{
    [super viewWillAppear];

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(newDataArrived:)
                name:NewDataArrivedNotification
              object:[NetworkListener sharedNetworkListener]];
}

请注意,完全相同的通知,解决同一个观察者,发件人和通知名称。

Note that it's exactly the same notification, resolving to the same observer, sender and notification name.

在这种情况下,如果我不删除其中一个addObserver调用,我将收到重复的通知给我的视图控制器。

In this case, if I don't remove one of those addObserver calls, I'll get duplicate notifications to my view controller.

在多线程环境中,这是一个受伤的世界。相信我。

In a multi-threaded environment, this is a world of hurt. Trust me.

只要将其放在那里以防有其他人遇到类似这样的事情。

Just putting this out there in case there are others who run into something like this.

这篇关于警告iOS / iPhone用户有关重复的NSNotification观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆