Executing methods one after another with pauses between executing
ios, iphone, method-call, objective-c, xcode
Solution
You could add these to an NSOperation queue...
NSOperationQueue *queue = [NSOperationQueue new];
queue.maxConcurrentOperationCount = 1;
[queue addOperationWithBlock:^{
[self method1];
}];
[queue addOperationWithBlock:^{
[NSThread sleepForTimeInterval:2.0];
[self method2];
}];
[queue addOperationWithBlock:^{
[NSThread sleepForTimeInterval:2.0];
[self method3];
}];
...
This will then run each one only after the previous one has finished and put the 2 second delay in for you.
Careful about using this to do an UI stuff though. This will run in a Background thread so you may need to deal with that.
Maybe this might work better you could do it by subclassing NSOperation but that's a lot of work for not much benefit.
Run this from where ever you want, I suggest putting all this into a function called setUpQueue or something.
Then from viewWillAppear or viewDidLoad or somewhere else, on a button press, etc... do...
[self setUpQueue];
All you have to do is add stuff to the queue, the queue will then manage itself.
Problem
Newbie obj-c question. I am writing a simple iPad presentation not for Appstore. My task is to implement few methods executed one after another with little pauses between them. Main structure looks like this: - view loads - two seconds pause, then executing method1 - two seconds pause, then executing method2 - two seconds pause, then executing method3 etc... First method I am calling from -viewDidLoad: ``` NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(firstCountStarts) userInfo:nil repeats:NO]; ``` Everything is ok here, method starts 2 seconds after view loads. From inside method1 I try to call method 2 in the same way but it start to execute simultaneously with method1. Same way triggered method3 (called from method2) and all methods after them not executed at all. I tried to situate all this methods in -ViewDidLoad and to call them with delays: ``` [self method1]; [self performSelector:@selector(method2) withObject:nil afterDelay:2]; [self performSelector:@selector(method3) withObject:nil afterDelay:4]; etc... ``` But after method2 is calling all methods after didn't executed. If I understand right the issue in threads. Do I need to use GCD to execute methods in different queues? Or maybe problem in else? Thanks, colleagues!