2

我正在使用带有 Core Data 的远程数据库,当我执行以下获取请求时,根据 Internet 连接,可能需要一些时间。我想监控这两个请求,当它们完成时——无论成功还是失败——我想触发另一种方法。

获取请求 1:

 [self.managedObjectContext executeFetchRequest:fetchRequest1 onSuccess:^(NSArray *results) {
        //Succcess
        [self.refreshControl endRefreshing];

    } onFailure:^(NSError *error) {
        [self.refreshControl endRefreshing];
    }];

获取请求 2:

 [self.managedObjectContext executeFetchRequest:fetchRequest2 onSuccess:^(NSArray *results) {
        //Succcess
        [self.refreshControl endRefreshing];

    } onFailure:^(NSError *error) {
        [self.refreshControl endRefreshing];
    }];

我想等到获取请求12都完成后再调用另一个方法。

我可以NSOperationQueue用来监控两个块吗?如果没有,知道两个块何时完成的最佳方法是什么?

4

1 回答 1

4

当您有具有依赖关系的异步任务时,您有几个选择:

  1. 代码更改最少的最简单解决方案是使用信号量:

    // create a semaphore
    
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    
    // initiate two requests (signaling when done)
    
    [self.managedObjectContext executeFetchRequest:fetchRequest1 onSuccess:^(NSArray *results) {
        [self.refreshControl endRefreshing];
        dispatch_semaphore_signal(semaphore);
    } onFailure:^(NSError *error) {
        [self.refreshControl endRefreshing];
        dispatch_semaphore_signal(semaphore);
    }];
    
    [self.managedObjectContext executeFetchRequest:fetchRequest2 onSuccess:^(NSArray *results) {
        [self.refreshControl endRefreshing];
        dispatch_semaphore_signal(semaphore);
    } onFailure:^(NSError *error) {
        [self.refreshControl endRefreshing];
        dispatch_semaphore_signal(semaphore);
    }];
    
    // now create task to to wait for these two to finish signal the semaphore
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
        // wait for the two signals from the two fetches to be sent
    
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    
        // now do whatever you want when those two requests finish
    
        // if you need to do any UI update or do any synchronizing with the main queue, just dispatch this to the main queue
    
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"all done");
        });
    });
    

    这种方法可能是最简单的,但会带来各种限制。例如,这将占用一个等待其他两个发送信号的工作线程,因此您必须确信您没有太多的这些请求集合同时进行。您还必须确信这些请求将调用onSuccess或调用onFailure,但绝不会调用两者,并且始终调用一个。这也没有真正提供取消机会或自己限制并发程度的能力。但是您可以通过最少的代码更改来完成上述操作。

  2. 第二种方法是将异步请求替换为同步请求,然后您可以使用NSOperation标准addDependency逻辑:

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    NSOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
        // completion operation
    }];
    
    NSOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        // do fetch1 _synchronously_
    }];
    
    [queue addOperation:operation1];
    
    NSOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
        // do fetch2 _synchronously_
    }];
    
    [queue addOperation:operation2];
    
    [completionOperation addDependencies:@[operation1, operation2]];
    [queue addOperation:completionOperation];
    

    这种方法要求您的同步提取是线程安全的。我不熟悉你正在使用的这个 API,所以我不能说。

  3. 如果您没有可以添加到队列中的 fetch 请求的同步再现,第三种方法是使用您自己的并发NSOperation子类包装异步 fetch 请求,该子类在异步操作完成之前不会发出信号isFinished(以及大概调用你自己的onSuccessonFailure块)。完成此操作后,您可以使用该setDependency功能(如前一点所示)使您的第三个操作依赖于其他两个完成。有关更多信息,请参阅并发编程指南的为并发执行配置操作部分。

我希望我能提供一个更明确的答案,但我对您的并发托管上下文库所包含的选项/约束不够熟悉。

于 2013-11-12T05:04:52.077 回答