0

How I call properly dispatch_async calls inside dispatch_async

dispatch_group_t downloadQueue = dispatch_group_create();
dispatch_group_async(downloadQueue,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {

NSLog(@"Main");
[NSThread sleepForTimeInterval:5.0];
NSLog(@"End");

    [self myMethod];

});
dispatch_group_notify(downloadQueue,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^ {

    NSLog(@"FINAL");

});

and here is myMethod :

-(void)myMethod
{
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
        // block1
        NSLog(@"Block1");
        [NSThread sleepForTimeInterval:5.0];
        NSLog(@"Block1 End");
    });
    dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
        // block2
        NSLog(@"Block2");
        [NSThread sleepForTimeInterval:8.0];
        NSLog(@"Block2 End");
    });

    dispatch_group_notify(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
        // block3
        NSLog(@"Block3");
    });
}

and my output is :
Main
End
Block1
Block2
FINAL
Block1 End
Block2 End
Block3

but i need
Main
End
Block1
Block2
Block1 End
Block2 End
Block3
FINAL

1
  • Your problem is that myMethod is asynchronous so it will dispatch its blocks and then return, exiting the outer dispatch group. Use dispatch_group_enter and dispatch_group_leave rather than dispatch_group_async Commented Nov 10, 2016 at 11:32

2 Answers 2

1

You just need to be notified with group of yours myMethod. Yours code will be:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {

    NSLog(@"Main");
    [NSThread sleepForTimeInterval:5.0];
    NSLog(@"End");

    dispatch_group_notify([self myMethod],dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^ {

        NSLog(@"FINAL");

    });
});

-(dispatch_group_t)myMethod{

    dispatch_group_t group = dispatch_group_create();

    dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
        // block1
        NSLog(@"Block1");
        [NSThread sleepForTimeInterval:5.0];
        NSLog(@"Block1 End");
    });


    dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
        // block2
        NSLog(@"Block2");
        [NSThread sleepForTimeInterval:8.0];
        NSLog(@"Block2 End");
    });

    dispatch_group_notify(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
        // block3
        NSLog(@"Block3");
    });
    return group;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use dispatch_group_wait in myMethod istead of dispatch_group_notify. This way your method will return only when both dispatch_group_async finish executing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.