Consider this code:
[self otherStuff];
// \"wait here...\" until something finishes
while(!self.someFlag){}
[self moreStuff];
Note that thi
This is a solution using a semaphore, be careful not to introduce a deadlock - you need some way of telling your application something has finished, you can either do that using the NSNotificationCentre
like you suggested but using a block is much easier.
[self someOtherStuffWithCompletion:nil];
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[self someOtherStuffWithCompletion:^{
dispatch_semaphore_signal(semaphore);
}];
NSLog(@"waiting");
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"finished");
[self someOtherStuffWithCompletion:nil];
I would suggest to use a NSOperationQueue and to wait for all tasks until they are finished at specific points. Something like that:
self.queue = [[NSOperationQueue alloc] init];
// Ensure a single thread
self.queue.maxConcurrentOperationCount = 1;
// Add the first bunch of methods
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method1) object:nil]];
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method2) object:nil]];
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method3) object:nil]];
// Wait here
[self.queue waitUntilAllOperationsAreFinished];
// Add next methods
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method4) object:nil]];
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method5) object:nil]];
// Wait here
[self.queue waitUntilAllOperationsAreFinished];
HTH