1

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

tshepang
  • 12,111
  • 21
  • 91
  • 136
Confused
  • 3,846
  • 7
  • 45
  • 72
  • look into blocks, or GCD – jcesarmobile Jun 06 '13 at 11:25
  • Just curious, would this work? `[[NSOperationQueue mainQueue] performSelector:@selector(subMethod1) onThread:[NSThread mainThread] withObject:self waitUntilDone:YES];` and on the next line call `subMethod2` normally. – matsr Jun 06 '13 at 12:06
  • 1
    @matsr, I think that might work, but also will block the UI, using completition blocks or GCD you won't block the UI – jcesarmobile Jun 06 '13 at 12:08

1 Answers1

5

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.

Bruno Koga
  • 3,864
  • 2
  • 34
  • 45
  • 1
    Your link is broken ;) the `_` before `index` at the end should not be there ;) Here's the correct link: http://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/index.html ;) – HAS Jun 06 '13 at 11:59