I use fast enumeration and in the enumeration block I send network requests asynchronously.
So what happens is the enumerateObjectsUsingBlock: just call the block super fast and let the enumeration block finish after some time.
This leads to different results, because some requests finish faster than other. So it’s not sorted as I wanted.
Is there any way to set the block to freeze, and after the asynchronous network requests is completed, to just tell it to go to the next one?
Here is some code
NSArray *sites = [self.fetchedResultsController.fetchedObjects copy];
NSLog(@"sites - %@",sites);
[sites enumerateObjectsUsingBlock:^(Sites *site, NSUInteger idx, BOOL *stop) {
NSLog(@"site name - %@,",site.name);
[[Wrapper sharedWrapper] sendRequestTo:site completionBlock:{
NSLog(@"site name - %@",site.name);
}];
}];
Thanks!
You can achieve that result by reorganizing your code: instead of using enumeration, just execute the async request from the completion block, one at the time:
Alternatives to this are modifying your
Wrapperclass so that it uses async networking (but use it on a secondary thread, then, to avoid blocking the UI).Or you might implement the Async Completion Token pattern so to be able to reorder the responses when they are received.