0
votes

I have three tasks. task1,task2 and task3. task1 and task2 are async tasks,i.e. they do task concurrently and the time of returning finish result is unpredictable . originally, I hope task1 and task2 do at same time,and after getting result to do task3.

dispatch_group_t dispatchGroup = dispatch_group_create();    

dispatch_group_async(dispatchGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   //do task1 async
});
dispatch_group_async(dispatchGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   //do task2 async
});
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){

            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    while (true) {
         sleep(0.2); //avoid cpu timeslice fully used
         //after get task1 and task2 result
         // do task3 
}});

I designed the structure as above,but I always get bad result,like if task2 is a url post request using afnetworking, I can't get success block involved.

anybody help me I will be appreciate.

2
do you use one thread or many for task1 and task2? - AleyRobotics
no,task1 and task2 are both url post requests using afnetwork. post requests are async . - Roby
I use dispatch_group_async(dispatchGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ }); .it is async. - Roby
better to use bool flags, because you can handle network errors and restart failed task again. - AleyRobotics

2 Answers

0
votes

i think there is a solution like this:

__weak CLASS * weakSelf = self;
bool task1Done;
bool task2Done;

FIRST ONE TASK
task1Done = NO;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

       do something at background....

        dispatch_async(dispatch_get_main_queue(), ^{
            do some thing at main thread...
        [weakSelf task1Done];
        });
    });

SECOND TASK
task2Done = NO;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

       do something at background....

        dispatch_async(dispatch_get_main_queue(), ^{
            do some thing at main thread...
        [weakSelf task2Done];
        });
    });


============
-(void) task1Done
{   
    task1Done = YES;
    if (task1Done && task2Done) [self startTask3];
}

-(void) task2Done
{   
    task2Done = YES;
    if (task1Done && task2Done) [self startTask3];
}

-(void) startTask3
{
.....
}
0
votes

ok, there is one more solution:

            dispatch_group_t group = dispatch_group_create();
            dispatch_queue_t queue1 = ...;
            dispatch_queue_t queue2 = ...;
            //add task to queue
            dispatch_group_async(group, queue1, ^{NSLog(@"Hello, World! TASK 1"); });
            dispatch_group_async(group, queue2, ^{NSLog(@"Hello, World! TASK 2"); });
//    wait forever
            dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
//    or wait with timeout
            dispatch_group_wait(group, DISPATCH_TIME_NOW);

            //Add notify for end of all tasks
            dispatch_group_notify(group, dispatch_get_main_queue(), ^{NSLog(@"Tasks 1 and 2 are done!");});

check if time is out

dispatch_time_t waitTime = dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC);
long waitResult = dispatch_group_wait(group, waitTime);

if (waitResult == 0)
{
    // all done!
} else
{
    // time out
}

do not forget release

dispatch_release(group);