iOS:在while循环中使用块回调的异步方法

时间:2015-02-19 17:46:12

标签: ios asynchronous while-loop objective-c-blocks breadth-first-search

我有以下要求:

给定一个分层树状结构,我正在执行breadth-first-search来遍历WHOLE数据集。 API通过以下方法提供数据:(使用AFNetworking向服务器发出请求,将结果保存到Core Data,并在存储的条目成功时回调完成块)

-(void) getChildrenForNodeId:(NSNumber*)nodeId completion:(void (^)(NSArray *nodes))completionBlock;

控制器执行以获取数据的方法:

 -(void)getAllNodesWithCompletion:(void (^)(NSArray *nodes))completionBlock{

     NSNumber *rootId = ...

     [MyNetworkManager getChildrenForNodeId:rootId completion:^(NSArray *nodes){

        for(Node *node in nodes){

             [self iterateOverNode:node.nodeId];

        }

       //execute completionBlock with nodes fetched from database that contain all their children until the very last leaf

     }];

  }

问题在于:

-(void)iterateOverNode:(NSNumber*)nodeId {

    NSMutableArray *elements = [NSMutableArray array];

    [elements addObject:nodeId];

    while ([elements count]) {

        NSNumber *current = [elements objectAtIndex:0];

        [MyNetworkManager getChildrenForNodeWithId:current completion:^(NSArray *nodes) {

              /**
                In order to continue with the loop the elements array must be updated. This can only happen once we have retrieved the children of the current node. 
However since this is in a loop, all of these requests will be sent off at the same time, thus unable to properly keep looping. 
          */
          for(Node *node in nodes){
              [elements addObject:node.nodeId];
          }

          [elements removeObjectAtIndex:0];

        }];

    }

}

基本上我需要回调的结果来控制while循环的流程,但我不知道如何实现它。我的理解是getChildrenForNodeWithId:completion:while-loop的请求应该在SERIAL订单的新线程中发生,以便另一个应该在第一个完成后开始。我不知道如何通过NSOperation和GCD实现这一目标。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

这里你需要的是一些递归。这个问题很棘手,因为我们还需要一种方法来跟踪和检测我们探索每个分支到叶节点的点。

我不是树搜索算法的专家,所以有些人可能会在这里改进我​​的答案。通过使用根节点id调用它来解决此问题。 self.trackingArray是具有NSMutableArray限定符的__block属性。每次我们开始对Node的请求时,我们将它的nodeId添加到这个数组中,当它返回时,我们删除它的nodeId,并添加它的nodeId的子节点。然后我们可以知道,当跟踪数组的计数达到0时,每个请求都返回,并且没有向数组添加子ID。一旦检测到我们完成了,您就可以调用已保存的块或委托方法。

此解决方案不包含任何错误处理。如果任何请求失败,则不会重试,并且将忽略所有子节点。

- (void)getNodesRecursively:(NSNumber *)nodeId {

    // Only do this once
    if (self.trackingArray == nil) {
        self.trackingArray = [NSMutableArray new];
        [self.trackingArray addObject:nodeId];
    }
    __weak typeof(self) weakSelf = self;

    [MyNetworkManager getChildrenForNodeWithId:nodeId completion:^(NSArray *nodes) {

        [self.trackingArray removeObject:nodeId];

        for (Node *node in nodes) {

            [weakSelf.trackingArray addObject:node.nodeId];
            [weakSelf getNodesRecursively:node.nodeId];
        }

        if (weakSelf.trackingArray.count == 0) {
            // We are done.
            // Reset the state of the trackingArray
            self.trackingArray = nil;

            // call a method here to notify who ever needs to know.
            [weakSelf allNodesComplete];
        }

    }];
}

您的其他方法看起来像这样

-(void)getAllNodesWithCompletion:(void (^)(NSArray *nodes))completionBlock{

     // Save the block to a property 
     self.completion = completionBlock;

     // Call -getNodesRecursively with root id
     [self getNodesRecursively:rootNodeId];
}

然后你可以有第二种方法

- (void)allNodesComplete {

      // Call saved block
      // Completion should load nodes from core data as needed
      self.completion();
}

我还没有对代码进行过测试,但这种做法是否合情合理?我假设我们不需要捕获这些节点,因为它们可以根据需要从核心数据加载。