0

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.

2 Answers 2

2

Fill the array with four NSNulls initially. In the handler, [yourArray replaceObjectAtIndex:tag withObject:responseObject].

Sign up to request clarification or add additional context in comments.

3 Comments

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.
@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].
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.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.