我有一个应用程序,其中用户操作尝试连接到在线资源。连接过程由thirdPartySDK完成,并且成功或失败通过异步方式返回。这由我配置为以任何一种方式发布通知的appDelegate处理。 (即Dropbox样式)。
在下面,假设失败,随着操作执行次数, UIAlertView 被调用多次。就是说,如果我反复测试连接失败,那么第一次调用该块一次,第二次调用该块两次,第三次调用3次,依此类推。就好像没有取消该块操作一样或从队列中删除。
if (!opQ) {
opQ = [[NSOperationQueue alloc] init];
}
[[NSNotificationCenter defaultCenter] addObserverForName:LINK_NOTIFICATION_FAILURE object:nil queue:opQ usingBlock:^(NSNotification *aNotification) {
dispatch_async(dispatch_get_main_queue(),
^{
[[[UIAlertView alloc] initWithTitle:@"Network_Account_Not_Linked" message:@"Your_attempt_to_link_your_account_ended_unsuccessfully"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
[[NSNotificationCenter defaultCenter] removeObserver:self];
});
}];
问题似乎出在 addObserverForName:object:queue:usingBlock:。我使用 addObserver:selector:name:object:进行了测试,该方法工作正常(每个通知执行一个选择器)。使用该块更加方便,使代码更易读并且可以访问局部变量,因此是我的动力。
我在NSBlockOperation线程中尝试了 [opQ cancelAllOperations] 和dispatch_async(根据调试器, opQ 在这一点上是“超出范围的”)。另外,还使用了带有类似失败结果的 [NSOperationQueue mainQueue] 。另外,在注册通知之前,我还尝试了从新的opQ开始。
为什么会发生多个通话?有使用块的更好方法吗?
最佳答案
关于使用方法的Apple文档说:
要取消注册观察,请将此方法返回的对象传递给removeObserver:。在释放由addObserverForName:object:queue:usingBlock:指定的任何对象之前,必须调用removeObserver:或removeObserver:name:object:。
换句话说,您不能仅在删除观察者时简单地传递self
。
尝试:
__block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:LINK_NOTIFICATION_FAILURE object:nil queue:opQ usingBlock:^(NSNotification *aNotification) {
dispatch_async(dispatch_get_main_queue(),
^{
[[[UIAlertView alloc] initWithTitle:@"Network_Account_Not_Linked" message:@"Your_attempt_to_link_your_account_ended_unsuccessfully"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
[[NSNotificationCenter defaultCenter] removeObserver:observer];
});
}];
关于ios - 通过addObserver:selector:name:object调用的多个 block 操作:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10689295/