I would like to call a method every 10 seconds, but I want to use something other than NSTimer. What could I use to do this?
-
4Why don't you want to use timers? An NSTimer seems like a perfect solution to this problem. – John Parker Apr 18 '11 at 15:56
-
As a note, Xcode is an IDE, not a framework or language, so please refrain from tagging your questions with it and placing it in the title unless you are asking about the IDE itself. – Brad Larson Apr 18 '11 at 17:41
5 Answers
I know you said you didn't want to use timers, but just to make sure you know how simple it would be with a timer...
[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(someMethod)
userInfo:nil
repeats:YES];

- 6,963
- 6
- 31
- 41
-
because I want that a ball start moving every ten second but I think aI can't have to timers for the same method – arvin Arabi Apr 18 '11 at 16:24
-
I don't think that's a problem - but even if it is, just create `anotherMethod` that calls `someMethod` :) – André Morujão Apr 18 '11 at 16:30
If you dont want to use the timer, you can use GCD which internally will make use of NSOperationQueue, nevertheless will work in all cases. For eg: i had a class which was inherited from NSOperation so the above methods didn't work so i had go go with GCD:
double delayInSeconds = 3.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(popTime, queue, ^{
[self methodYouWantToCall];
});
The above code calls the method methodYouWantToCall after every three seconds.
-
1"GCD which internally will make use of NSOperationQueue" This is not correct. It is vice versa. – Michael Dorner Sep 07 '15 at 14:23
You can create a loop with performSelector:withObject:afterDelay:
setting afterDelay to 10.0.
I don't recommend this though, use an NSTimer.
- (void)callMeEvery10Seconds
{
[self performSelector:@selector(callMeEvery10Seconds)
withObject:nil
afterDelay:10.0];
// ... code comes here ...
}

- 47,228
- 12
- 98
- 108
The easiest way to do so is:
- (void)scheduleLoopInSeconds:(NSTimeInterval)delayInSeconds
{
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(popTime, queue, ^{
[self callWhatEverMethodYouWant];
[self shceduleLoopcaInSeconds:delayInSeconds];//set next iteration
});
}
// now whenever you like call this, and it will be triggering "callWhatEverMethodYouWant" every 10 secs.
[self shceduleLoopcaInSeconds:10.0];
-
WOW, I have tested this method and it works ok but the CPU usage that was around 25% with NSTimer skyrocketed to 99%!!! WTF? I guess this is called by the overhead associated with dispatching the blocks! – Duck Apr 22 '17 at 23:21
If you are not using Cocos2D, you have to use a NSTimer to do this....
If you are using Cocos2D, use the schedule method
here's a link below that shows both :
-
Why would you "have" to use an NSTimer if you're not using Cocos2D? – André Morujão Apr 18 '11 at 16:06