iOS:使用在后台运行的方法进行For循环?

Kin*_*gon 1 for-loop objective-c ios objective-c-blocks

我有一个id的数组,我在其中运行for循环,并调用在后台运行的方法(进行网络调用以获取数据).我如何知道for循环何时完成以及所有调用都已完成?

目前,for循环在所有预期的呼叫完成之前完成.我应该使用while循环吗?

更新(终于搞定了!)

我在这里发布了我的代码,希望它能帮助别人.:)

详细信息:根据Andrey的建议,我做的第一件事是继承NSOperation并执行以下操作:

@interface AsyncOperation ()

@property (atomic, assign) BOOL _executing;
@property (atomic, assign) BOOL _finished;

@end

@implementation AsyncOperation

- (void)start {

    if ([self isCancelled]) {

        //Move the operation to the finished state if it is canceled.
        [self willChangeValueForKey:@"isFinished"];
        self._finished = YES;
        [self didChangeValueForKey:@"isFinished"];
        return;
    }

    //If the operation is not canceled, begin executing the task.
    [self willChangeValueForKey:@"isExecuting"];
    [NSThread detachNewThreadSelector:@selector(main) toTarget:self withObject:nil];
    self._executing = YES;
    [self didChangeValueForKey:@"isExecuting"];

}

- (void)main {
    if ([self isCancelled]) {
        return;
    }
}

- (BOOL)isAsynchronous {
    return YES;
}

- (BOOL)isExecuting {
    return self._executing;
}

- (BOOL)isFinished {
    return self._finished;
}

//Make sure to expose this method in the header file
- (void)completeOperation {

    [self willChangeValueForKey:@"isFinished"];
    [self willChangeValueForKey:@"isExecuting"];

    self._executing = NO;
    self._finished = YES;

    [self didChangeValueForKey:@"isExecuting"];
    [self didChangeValueForKey:@"isFinished"];
}
Run Code Online (Sandbox Code Playgroud)

最后,在我的方法中,我做了:

    //Setup NSOperation Queue
    self.queue = [[NSOperationQueue alloc] init];
    self.queue.name = @"myQueue";

    //Set Queue Observer (Using Third Party Library KVOController)
    [self.KVOController observe:self.queue keyPath:@"operationCount" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew block:^(id observer, NSOperationQueue *queue, NSDictionary *change) {

        //Check if all operations concluded
        if (queue.operationCount == 0) {

            //Remove Observer
            [self.KVOController unobserve:self.queue];

            //DO SOMETHING NOW THAT EVERYTHING IS DONE
        }
    }];

    //Loop through my chats and fetch each one
    for (NSString *chatId in chatIds) {

        //Observe Operation
        AsyncOperation *operation = [[AsyncOperation alloc] init];
        [self.KVOController observe:operation keyPath:@"isExecuting" options:NSKeyValueObservingOptionNew block:^(id observer, EMKAsyncOperation *myOperation, NSDictionary *change) {

            //Check if operation is executing
            if ([myOperation isExecuting]) {

                //Fetch Chat (My async method)
                [self fetchChatWithId:chatId inBackgroundWithBlock:^(Chat *chat, BOOL succeeded, NSError *error) {

                    //Check for error
                    if (!error) {

                        //DO WHATEVER I DO AFTER FETCHING COMPLETED

                        //Complete operation manually 
                        [myOperation completeOperation];

                        //Remove Observer
                        [self.KVOController unobserve:myOperation];

                    } else {

                       //Error Cancel Everything
                       finalError = error;

                       NSLog(@"Error fetching all chats: %@", [error localizedDescription]);

                       //Cancel Operations
                       [self.queue cancelAllOperations];

                        //Complete operation
                        [myOperation completeOperation];

                        //Remove Observer
                        [self.KVOController unobserve:myOperation];
                    } 
                }];
            }
        }];

        //Add Operation
        [self.queue addOperation:operation];
    }
Run Code Online (Sandbox Code Playgroud)

希望这有助于人们!

And*_*kha 5

看起来问题不在于找出循环何时完成,这很明显,而是在所有网络调用完成时.所以这就是我要做的.我将每个网络调用包装到一个NSOperation类的实例中,并将每个操作添加到一个NSOperationQueue.此类具有operationsCount可观察键值的属性.因此,使用KVO可以观察到此属性的变化.一旦operationsCount等于0 - 你就完成了.所有请求都已完成