I’m trying to enforce a specific order for tasks to complete using Grand Central Dispatch but I’m having a bit of trouble understanding the correct way to do it. I tried using groups in the following way:
Initialization:
startup = dispatch_group_create();
Tasks that need to wait:
//Don't want to wait on the main thread, so dispatch async to a concurrent queue
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0L),^{
//Wait until we're finished starting up
dispatch_group_wait(startup,DISPATCH_TIME_FOREVER);
//Now we can do this stuff back on the main queue
dispatch_async(dispatch_get_main_queue(),^{
//Do work
});
});
Work that I need to wait for:
dispatch_group_async(startup,dispatch_get_main_queue(),^{ // work });
Due to the nature of my app, the tasks that need to wait can occur BEFORE the work that I need to wait for. What I really want is the ability to wait on a condition that way when the condition is done, it’s done, and all future threads can do their thing. Does GCD have this?
Works if I use a semaphore and then signal after each call to wait.
Also works if I call dispatch_group_enter and dispatch_group_leave.