Это конкретная форма общего вопроса, которая называется «как я могу назвать последовательность операций с блоками и получать уведомление, когда последний заканчивается?»
Одна из идей - создать «список дел» с использованием параметров для каждого запроса. Скажем, каждый запрос занимает число 0..99. Теперь псевдо-код будет выглядеть следующим образом:
@property(nonatomic, copy) void (^done)(BOOL); // we'll need to save a completion block
@property(nonatomic, strong) NSMutableArray *todo; // might as well save this too
- (void)makeRequestsThenInvoke:(void (^)(BOOL))done {
self.todo = [NSMutableArray arrayWithArray:@[@99, @98, @97 ... @0]];
// make this in a loop using real params to your network request (whatever distinguishes each request)
self.done = done;
[self makeRequests];
}
- (void)makeRequests {
if (!self.todo.count) { // nothing todo? then we're done
self.done(YES);
self.done = nil; // avoid caller-side retain cycle
return;
}
// otherwise, get the next item todo
NSNumber *param = [self.todo lastObject];
// build a url with param, e.g. http://myservice.com/request?param=%@ <- param goes there
[afManager post:url success:success:^(AFHTTPRequestOperation *operation, id responseObject) {
// handle the result
// now update the todo list
[self.todo removeLastObject];
// call ourself to do more, but use performSelector so we don't wind up the stack
[self performSelector:@selector(makeRequests) withObject:nil afterDelay:0.0];
}];
}
Незначительная проблема - ваш селектор называется 'makeRequests', но вы вызываете' makeRequests: '. –
Ах. Благодарю. Будет редактировать. – danh