0

I added autostart in my iOS application (add VoIP in Required background modes) and it worked fine. Then I wanted to add NSTimer. I wrote the following in - (id) init

NSLog(@"!!!!IT'S START!!!!");

    MyTimer = [NSTimer scheduledTimerWithTimeInterval:60 target:self
                                                     selector:@selector(StartMyTimer) userInfo:nil repeats:YES];


- (BOOL) StartMyTimer
{
NSLog(@"Timer is wirk");
}

When my app is autostarted I get a message "!!!!IT'S START!!!!" and no one...

If some one knows what the problem is then please help!

Dean
  • 939
  • 10
  • 30
user1786639
  • 163
  • 11

2 Answers2

0

First, maybe you want to check your English in those log messages.

Second, by convention you should be using lowercase (camelCase) names for selectors, and for variables as well ("myTimer" rather than "MyTimer").

Third, the BOOL return type does not really make sense. Nor does the method name startMyTimer. Perhaps something like timerFired or might be more appropriate.

Finally, you have to make sure your app is still running, otherwise the timer will not be able to call the method. Also, you have to make sure the timer is not deallocated, so make sure the class that contains it is in memory. You should maybe also keep a strong reference to the timer.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • > Also, you have to make sure the timer is not deallocated, so make sure the class that contains it is in memory And how I can do it?.... What you mean "strong reference"? Thanks! – user1786639 Dec 04 '13 at 21:29
  • For example, your app delegate could keep the `NSTimer` object as a variable. Then you can be sure it is always in memory (until you destroy it explicitly). – Mundi Dec 04 '13 at 22:39
  • scheduled timers are not deallocated even if you don't keep the reference. The same applies for their target object. – Sulthan Dec 04 '13 at 23:17
  • Still - it is useful to keep the timer reference to invalidate it if necessary. I could not make it work with a deallocated instance. – Mundi Dec 05 '13 at 00:28
0

Maybe you must add parameter to receive the timer and add : at the end of StartMyTimer.

MyTimer = [NSTimer scheduledTimerWithTimeInterval:60 target:self
                                                 selector:@selector(StartMyTimer:) userInfo:nil     repeats:YES];


- (BOOL) StartMyTimer:(NSTimer *)myTimer {
    NSLog(@"Timer is wirk");
 }
Beto
  • 3,438
  • 5
  • 30
  • 37