Jon
Jon

Reputation: 4732

Add objects to array in a certain order?

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.

Upvotes: 0

Views: 92

Answers (2)

Chuck
Chuck

Reputation: 237030

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

Upvotes: 2

Rayfleck
Rayfleck

Reputation: 12106

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.

Upvotes: 2

Related Questions