Search code examples
objective-cmethodscompletion-block

How to make a method to wait till another method complete it work in Objective C?


Consider this scenario.

-(void) mainMethod
{
   [self subMethod1];
   [self subMethod2];
}

My assumption:

When calling the mainMethod, it will call the subMethod1 first, then the subMethod2. But, there is no rule that says "After completing the subMethod1 only, the subMethod2 should get called". If I use some delay lines or some animation with duration in subMethod1, the control won't wait for it completion. It will just start the next process. So, the subMethod2 can be called before the subMethod1 completes its work. Am I right?

Question:

How can I make subMethod2 to wait, till subMethod1 completes its work?

Note: I don't want to place subMethod2 into subMethod1


Solution

  • You should use a completion block (like UIView animations have blocks that are executed after the animations happen).

    If you're not familiar with blocks, take a look at Apple's Short Practical Guide to Blocks.