I have a 4 async network requests with a completion block. Upon completion, i want them to be added to a master NSMutableArray is the order that the requests were sent, but not the order they were received (as this may be different). I have a tag that I set when sending the request so I can use this tag in the completion block.
Add a comment
|
2 Answers
Fill the array with four NSNulls initially. In the handler, [yourArray replaceObjectAtIndex:tag withObject:responseObject].
3 Comments
Jon
in
viewDidLoad, I did: self.masterArray = [NSMutableArray arrayWithObjects:NULL, NULL, NULL, NULL, nil]; and in the completion block i have: [self.masterArray replaceObjectAtIndex:aReceipt.tag withObject:[aSearchObjectType objectsFromServerDictionaries:aResultsArray]];. I'm getting an exception though.Chuck
@Jon: NULL is the same thing as nil, and isn't legal in an array. What you want to use here is NSNull, which is obtained through
[NSNull null].Jon
One last thing, if there are no results form the server, the array is not modified, so I need to check for this in
numberOfRowsInSection. I tried NSArray *sectionArray = [self.masterArray objectAtIndex:iSection]; if (sectionArray == [NSNull null]) { return 1; } else { return sectionArray.count; }, but that null comparison doesn't work.I assume you don't know how many elements each request will return, so you'll have to store all four of your result sets temporarily until all four requests are finished. (An NSMutableDictionary, keyed by the tag would make sense.) Then, you can iterate through the dictionary's keys and append those objectForKeys (arrays) to your mutable array in the correct order.