Reputation: 4183
I use following code to retrive the upload order of files. The files must be uploaded in this specific order and cannot be uploaded simultaneously. Therefor I cannot put my code in this for-loop because it would hang the thread.
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"order" ascending:YES];
NSArray *sortedArray = [agenciescontroller.arrangedObjects sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
for (NSManagedObject *a in sortedArray)
{
for (NSMutableDictionary *d in imagescontroller.arrangedObjects)
{
if ([[NSString stringWithFormat:@"%@",[d valueForKey:[a valueForKey:@"agency"]]] isEqualToString: @"1"])
{
///////////////////////////UPLOAD FILES HERE/////////////////////////////
checkedchecks += 1;
NSLog(@"Uploading %@ to %@", [d valueForKey:@"filename"], [a valueForKey:@"agency"]);
}
}
}
My idea was to use this for loop to populate a NSMutableArray
or NSMutableDictionaries
holding the precise list of files to upload and then go trough this list one by one. Is this a wise idea or is there a better solution? Also keep in mind that once the file is uploaded, I have to update a value inside the mutable dictionary in the foor loop. Where I check for a value = "1" that specific value has to be set to 0 once the upload is finished.
Upvotes: 0
Views: 54
Reputation:
Use a NSOperationQueue
where you set the maxConcurrentOperationCount
property to 1. Then add one NSOperation
per upload in your existing loop.
To update the value in your dictionary you can add a completion block to your NSOperation
objects. Just make sure to use the necessary synchronization when updating the value since the completion block will run on some background thread.
Upvotes: 1