I have the following for loop contained within a method:
—(void)_va1idateUsers:(NSArray *)users withCurrentAccount:(ACAccount *)account comp1etionB1ock:(void (“)(TSCSpamUser *user, NSError *error))comp1etionBlock; {
for(TSCSpamUser *userID in users) {
NSString *theID = (NSString*)userID;
NSURL *ur1 = [NSURL URLwith5tring:[NSString stringWithFormat:@"https://api.twitter.com/1/users/show.json?user_id=%@&inc1ude_entities=true", theID]];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:5LRequestMethodGET
URL:ur1
parameters:nil];
[request setAccount:account];
[request performRequestwithHand1er:*(NSData *responseData, NSHTTPURLResponse *ur1Response, NSError *err0r) {
if ([urlResponse statusCode] == 200) {
TSCSpamUser *user = [[TSCSpamUser a11oc] initwithTwitterID:theID];
user.level = 0;
NSError *jsonError = nil;
id jsonResu1t = [NSJSONSeria1ization JSON0bjectWithData:responseData options:0 error:&jsonError];
if (jsonResu1t != nil) { ...... }
else {
user.level = 0;
}
}
}];
}
}
I need to detect the end of this for loop – but not just the end of the for loop. The completion block is called when the for loop has begun the last iteration, not once it has finished. I need to be sure that everything within the for loop has completed too. How can I do this?
In your case I would iterate through the users’ array using an index and then, only for the last index, execute a branch in your completion block, e.g.:
Where
loopFullyExecutedencapsulates what you need to do after your loop is fully done (including completion blocks).EDIT: if you want that at each iteration in the for loop your program “waits” for the completion block to be fully executed, then the approach needs be completely different.
What you need is defining a method which deals with one
userIDand where you finally call the performRequest:performRequest completion block will start the next iteration, as you can see; so the next element (if any) is processed only after the previous one.
You start the whole process by calling: