0

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?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
arvin Arabi
  • 253
  • 1
  • 7
  • 21
  • 4
    Why 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 Answers5

11

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];
André Morujão
  • 6,963
  • 6
  • 31
  • 41
3

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
Apple D
  • 31
  • 2
2

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 ...
}
Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
0

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];
Duck
  • 34,902
  • 47
  • 248
  • 470
Loebre
  • 615
  • 2
  • 8
  • 23
  • 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
0

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 :

How can I create a count down timer for cocos2d?

Community
  • 1
  • 1
DJ Burb
  • 2,346
  • 2
  • 29
  • 38