ios - 等待 block 完成

标签 ios objective-c objective-c-blocks

<分区>

我有一个 UITableView,我正在尝试获取行数。但是,我在使用 block 时遇到了问题。在下面的代码中,我只想返回计数,但据我所知, block 是异步的。我环顾四周试图找到解决方案,但没有一个有效。我尝试过的一种解决方案是:How do I wait for an asynchronously dispatched block to finish?但是当我点击按钮进入带有表格的 View 时,点击按钮时它就卡住了。我尝试了其他一些,但它们也没有用。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  GlobalVars *globals = [GlobalVars sharedInstance];

  __block int count = 0;
  GKLocalPlayer *localPlayer = [[GameCenterHelper sharedInstance] getLocalPlayer];
  [[GameCenterHelper sharedInstance] getMatches:^(NSArray *matches) {
    NSLog(@"Matches: %@", matches);
    for (GKTurnBasedMatch *match in matches) {
      for (GKTurnBasedParticipant *participant in match.participants) {
        if ([participant.playerID isEqualToString:localPlayer.playerID]) {
          if (participant.status == GKTurnBasedParticipantStatusInvited) {
            [globals.matchesReceived addObject:match];
            count++;
            NSLog(@"INVITED");
          }
        }
      }
    }
  }];

  return count;
}

有人可以帮助我正确返回count吗?

最佳答案

您应该使用回调 block 。不要试图让异步代码同步运行。

此外,无需让您的 GlobalVars 单例保留匹配数组。这可能被认为是糟糕的设计。

typedef void(^CallbackBlock)(id value);

- (void)viewDidLoad {
    [super viewDidLoad];
    //show some sort of loading "spinner" here
    [self loadMatchesWithCallback:(NSArray *matches) {
        //dismiss the loading "spinner" here
        self.matches = matches;
        [self.tableView reloadData];
    }];
}

- (void)loadMatchesWithCallback:(CallbackBlock)callback {
    GlobalVars *globals = [GlobalVars sharedInstance];
    GKLocalPlayer *localPlayer = [[GameCenterHelper sharedInstance] getLocalPlayer];
    [[GameCenterHelper sharedInstance] getMatches:^(NSArray *matches) {
        NSLog(@"Matches: %@", matches);
        NSMutableArray *filteredMatches = [NSMutableArray array];
        for (GKTurnBasedMatch *match in matches) {
            for (GKTurnBasedParticipant *participant in match.participants) {
                if ([participant.playerID isEqualToString:localPlayer.playerID]) {
                    if (participant.status == GKTurnBasedParticipantStatusInvited) {
                        [filteredMatches addObject:match];
                        break; //you don't want to add multiples of the same match do you?
                    }
                }
            }
        }
        if (callback) callback(filteredMatches);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.matches.count;
}

关于ios - 等待 block 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24870458/

上一篇:ios - 单例不返回任何图像

下一篇:ios - 带有 openssl 加密数据的 NSString initWithBytes 使用 NSUTF8StringEncoding 返回 nil

相关文章:

iOS - 调用 dispatch_async 时没有堆栈跟踪

iphone - 将 block 与 NSOperation 一起使用

ios - UIView 的屏蔽区域仍然可以触摸吗?

ios - 如何使用json和swift 4.1在Google map 上添加多个标记?

ios - 仅从 NSXMLElement 获取文本值,不带子节点的文本

objective-c - 在 Objective-C block 中使用 typeof(self) 声明强引用

ios - 在 iOS 应用程序中的什么位置放置设备特定值?

ios - 自 iOS 10 以来,带有 startMonitoringForRegion 的 CCLocation 不再工作

iphone - @property 中的内存损坏

ios - 禁用 UIScrollView 手势