Я хочу выполнить серию AFJSONRequestOperation по порядку и иметь возможность прерывать очередь в случае сбоя.
На данный момент то, как я это делаю, ненадежно, так как иногда может начаться следующая операция.
У меня есть синглтон для вызова моей конечной точки API
AFJSONRequestOperation *lastOperation; // Used to add dependency
NSMutableArray *operations = [NSMutableArray array]; // Operations stack
AFAPIClient *httpClient = [AFAPIClient sharedClient];
[[httpClient operationQueue] setMaxConcurrentOperationCount:1]; // One by one
И затем я добавляю операции таким образом
NSMutableURLRequest *request = ...; // define request
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// Takes care of success
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
[[httpClient operationQueue] setSuspended:YES];
[[httpClient operationQueue] cancelAllOperations];
}];
[push:operation addDependency:lastOperation];
[operations $push:operation]; // This is using ConciseKit
lastOperation = operation;
// Repeat with other operations
// Enqueue a batch of operations
[httpClient enqueueBatchOfHTTPRequestOperations:operations ...
Проблема в том, что иногда операция, следующая за неудачной, все еще получает шанс начаться.
Таким образом, кажется, что наличия 1 параллельной операции max и цепочки зависимостей недостаточно, чтобы сказать очереди ждать до тех пор, пока обратный вызов сбоя не будет полностью выполнен.
Каков правильный способ сделать это?
Спасибо