ios - 通过addObserver:selector:name:object调用的多个 block 操作:

标签 ios nsoperationqueue nsnotificationcenter

我有一个应用程序,其中用户操作尝试连接到在线资源。连接过程由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/

相关文章:

ios - 创建一个自定义的可滚动 Collection View ,模仿 Swift 中 UITableView 的行为

android - React Native 中 redux over normal state 机制的用例

objective-c - NSOperationQueue 中的线程命名

ios - 应用程序崩溃,未收到内存警告

javascript - javascript 中 RTCMultiConnection 的等效 ios 库

ios - SwiftUI:.fileImporter 修改器在通过点击文件选择器 "outside"关闭时不更新绑定(bind)

multithreading - 主线程上的 NSFetchedResultsController 忽略从不同线程保存到核心数据?

objective-c - AFNetworking:为更高优先级的请求中断后台请求

ios - KVO和NSNotificationCenter的正确用法和区别是什么?

ios - 类型“NSNotification.Name”没有成员“keyboardDidShowNotification”