Can someone help,
I have a NSMutablearray which contains approx a 1000 data entries.
What i want to do is go though these entries one by one and send them to a server with HTTP request.
However its important that i only send them one at a time, so i have the following pseudo code
for(int i =0; i < array.count; i++)
{
[ServerLayer ServerUploadRow:[array objectAtIndex:i] : ^void (int ReturnValue, BOOL err){
if(err == true)
{
//Do some local stuff here.
}
}];
}
what i ideally want to do is having something like the following
for(int i =0; i < array.count; i++)
{
//Wait here till i know that we not waiting on a block to complete
[ServerLayer ServerUploadRow:[array objectAtIndex:i] : ^void (int ReturnValue, BOOL err){
if(err == true)
{
//Do some local stuff here.
//Signify that we done
}
}];
}
the function ServerUploadRow just sends HTTP command.
Thanks
Assuming that ServerUploadRow
runs asynchronously, you can have the request initiate the next request in the completion block of the previous one:
- (void)initiateRequestNumber:(NSInteger)index
{
[ServerLayer ServerUploadRow:array[index] : ^(int ReturnValue, BOOL err){
if (err == true) {
//Do some local stuff here.
} else {
NSInteger nextIndex = index + 1;
if (nextIndex < array.count) {
[self initiateRequestNumber:nextIndex];
} else {
// do whatever you want now that everything is done
}
}
}];
}
And you'd start this with:
[self initiateRequestNumber:0];
If you can issue these requests concurrently, or better combine all of this in one request, it will be much faster. I'd suggest considering refactoring your web service to enable this, if at all possible. But submitting 1000 network requests sequentially is going to horribly slow (as you'll suffer network latency 1000 times).
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加