views:

54

answers:

3

I want to be able to call a method and then, either wait for the method to finish doing it's thing and then invoke the next method call, OR call a method, wait a certain amount of time and then invoke the next method.

Any thoughts on how I would do that?

Example:

[self method1];
//wait for method1 to finish whatever it's doing then move onto the next line
[self method2];

OR

[self method1];
//wait say, 500ms then move onto the next line
[self method2];
+1  A: 

with : performSelector:withObject:afterDelay:

Deniz Mert Edincik
and how about if I wanted to wait for the first method to finish?
cgossain
performSelectorOnMainThread:withObject:waitUntilDone:
Deniz Mert Edincik
Awesome seems to be working, only problem is that i'm using a 100ms delay. When I use a 200ms delay everything works fine. Is there a way i can measure the time it take for the method to complete?
cgossain
@cgossain - Sometimes, timers will not be completely accurate. This has to do with CPU cycles and low level stuff. See the NSTimer reference for more.
Moshe
+1  A: 

If you want to make sure that method has finished doing what it does, why not either

a) call method2 at the end of method1

or

b) go with performSelectorOnMainThread:withObject:waitUntilDone: as suggested by Deniz Mert Edincik

or

c) Send an NSNotification at the end of method1 to trigger method2 (you could add an observer for that notification in method1 and remove it again in method2, if method1 is also called elsewhere and you do not want method2 triggered every time)

You should not work with afterDelay, relying on whatever delay you specify...

Joseph Tura
A: 

Break your code in half (e.g. into multiple methods).

    ...
    [self method1];
    return;
}

- (void) secondHalf {
    [self method2];
    ...

To wait for method1 to finish, exit/quit/return from your 1st part of your code back to the run loop. Have something else start the latter half of your method (notification, delegation, timer, etc.). Save any needed local variables or other state in instance variables.

hotpaw2