使用NSNotificationCenter,它一直运行良好,直到我开始向其他类发送非常快的消息。
什么是快速的?其中大约30-40个通知。我甚至不会得到其中的一个。 还有其他办法吗? 我应该更新全球而不是吗?
//post data out .
- (void)post:(NSString*)string
{
NSLog(@"done"); //the log is printing
NSDictionary *userInfo = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:@"connector"
object:string
userInfo:userInfo];
}
我知道观察者是好的,因为之前的代码相同。 非常感谢。
答案 0 :(得分:3)
如果您每秒发送30-40个通知,您应该再次考虑您的实施。
以下是一些替代方案:
NSNotificationCentre的问题在于它将消息发送给每个观察者 - 这可能会变慢,通常用于更新状态更改视图(登录/注销)。
答案 1 :(得分:2)
看起来您设置了错误的object
,这应该是发布通知的对象(或nil
)。我认为您应该将string
添加到userInfo
:
NSDictionary *userInfo = @{@"somekey" : string };
[[NSNotificationCenter defaultCenter] postNotificationName:@"connector"
object:self
userInfo:userInfo];