0

I have 3 function in my code. The code i did for manage queue for function execution.

 [self firstMethodWithOnComplete:^{
    [self SecongMethodWithOnComplete:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(CallThirdMethod) userInfo:nil repeats:NO];
            });
    }];
}];

First And Second Function

- (void)firstMethodWithOnComplete:(void (^)(void))onComplete {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    //processing here.....

    [self CallFirstMethod];
    onComplete();
});
 }

- (void)SecongMethodWithOnComplete:(void (^)(void))onComplete {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    //processing here.....
    [self CallSecondMethod];
    onComplete();
});
 }

The problem is i am unable to manage their execution. I want execution order such in a way that second function only execute if first is over and third execute if second execution over. Please help me to solve this or give any appropriate suggestions.

Mitesh Dobareeya
  • 970
  • 1
  • 11
  • 36
  • dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"dail image : %@",self.imageUrl); self.imgAppIcon.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.imageUrl]]]; }); }); – Birendra Mar 22 '16 at 07:03
  • you can try like this – Birendra Mar 22 '16 at 07:03
  • Have a read of this article: http://stackoverflow.com/questions/7936570/objective-c-pass-block-as-parameter/27379471#27379471 It shows how to call a function (loading data from a website) which then calls a second function once it's finished. It shows how to pass a function as a parameter. This should point you in the right direction. – Mike Gledhill Mar 22 '16 at 07:44

1 Answers1

1

You can use dispatch groups for this kind of requirement, Below i am posting example code which i have used

__block NSError *configError = nil;
__block NSError *preferenceError = nil;

// Create the dispatch group
dispatch_group_t serviceGroup = dispatch_group_create();


dispatch_group_enter(serviceGroup);
 // Start the first async service
dispatch_group_leave(serviceGroup);


dispatch_group_enter(serviceGroup);
// Start the second  async service
dispatch_group_leave(serviceGroup);

dispatch_group_notify(serviceGroup,dispatch_get_main_queue(),^{
    //Update UI in this block of code
});
Mukesh
  • 3,680
  • 1
  • 15
  • 32