I have an asynchronous method longRunningMethodOnObject:completion:
this method receives an object of type 'Object' - does work with its data and then calls the completion handler.
I need to call many different "longRunningMethods" and wait for all to complete.
I would like all of the "longRunningMethodOnObject" to run asynchronously (parallel) to each other in the "for" loop. (I am not certain if the "longRunningMethodOnObject" runs in serial to each other but this is more of a general question)
I'm not sure I have created a proper semaphore and would appreciate an explanation on the proper way to synchronize between them.
The code for the wrapper function is as follows:
-(void)applyLongRunningOperationOnObjectInArray:(NSArray *)theObjects completion:(completion)block
{
// offsetting to worker thread in my object
dispatch_async(self.myQueue,^{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); //Not sure if this should be here or in the for loop
for(Object *ob in theObjects)
{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); // the semaphore for the "for" loop - should this be here? or above the for loop
[GlobalObject longRunningMethodOnObject:ob completion:^{ // would like each call to be independant of previous call in for loop
dispatch_semaphore_signal(semaphore); // signaling that it completed
}];
}
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); // would like to wait here for each semaphore
block(); // invoke the completion handler
});
}