Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios: waiting for method to finish executing before continuing

I am new to IOS development and am currently facing a problem.

When method A is called, it calls method B and then it wait for delegate connectionDidFinish which connectionDidFinish will execute MethodC.

My question is how do I ensure that methodA to methodC has finished executing before executing NSLog?

I found that a way to solve this problem is to use notification center. Send notification to me after finishing executing methodC. I don't think this is a good solution. Is there another way to do this?

Example:

 [a methodA];
 NSLog(@"FINISH");
like image 676
user3093525 Avatar asked Oct 14 '25 03:10

user3093525


1 Answers

If any of those methods perform actions asynchronously, you can't. You'll have to look into a different way of doing this. I personally try to use completion blocks when ever I can, although it's perfectly fine to do this other ways, like with delegate methods. Here's a basic example using a completion block.

- (void)someMethod
{
    [self methodAWithCompletion:^(BOOL success) {
        // check if thing worked.
    }];
}

- (void)methodAWithCompletion:(void (^) (BOOL success))completion
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, kNilOptions), ^{

        // go do something asynchronous...

        dispatch_async(dispatch_get_main_queue(), ^{

            completion(ifThingWorked)

        });
    });
}
like image 129
Mick MacCallum Avatar answered Oct 20 '25 09:10

Mick MacCallum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!