iPhone - NSTimer not repeating after fire

一笑奈何 提交于 2019-12-21 07:55:39

问题


I am creating and firing a NSTimer with:

ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                           target:self
                                         selector:@selector(handleTimer:)
                                         userInfo:nil
                                          repeats:YES];
[ncTimer fire];

AND

- (void)handleTimer:(NSTimer *)chkTimer {
    // do stuff
}

I am retaining my timer with:

@property (nonatomic, retain) NSTimer *ncTimer;

For some reason the timer is not repeating. It is firing once only and than never again.


回答1:


You can't just assign to the timer that you have put as a property in your header. This should work:

self.ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self selector:@selector(handleTimer:) userInfo:nil repeats: YES];

Also: The fire method fires the timer, out of cycle. If the timer is non repeating it is invalidated. After the line that says fire, add this:


BOOL timerState = [ncTimer isValid];
NSLog(@"Timer Validity is: %@", timerState?@"YES":@"NO");



回答2:


The -fire: method manually fires it once. For a timer to be started and repeat, you have to add it to a runloop using [[NSRunLoop currentRunLoop] addTimer: forMode:]




回答3:


Got it

Adding timer to mainRunLoop made it working 😆😆😆

Here you go:

Objective C:

self.ncTimer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

Swift 2

var ncTimer = NSTimer(timeInterval: 2.0, target: self, selector: Selector("handleTimer"), userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(ncTimer, forMode: NSDefaultRunLoopMode)

Swift 3, 4, 5

var ncTimer = Timer(timeInterval: 2.0, target: self, selector: #selector(self.handleTimer), userInfo: nil, repeats: true)
RunLoop.main.add(ncTimer, forMode: RunLoop.Mode.default)



回答4:


You can also copy your code inside this block, which inserts the creation of the Timer in the main thread.

The code will therefore remain:

dispatch_async(dispatch_get_main_queue(), ^{
  self.ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                 target:self selector:@selector(handleTimer:) userInfo:nil repeats: YES];
});



回答5:


Assigning to ncTimer as you have will not initiate the properties retain functionality.

Assuming the declaration is within the member object you will need to do:

self.ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES]



回答6:


I don't know why but Timer.scheduledTimer method is not working but Timer.init method worked.

self.timer = Timer.init(timeInterval: 10.0, repeats: true, block: { (timer) in
            print("\n--------------------TIMER FIRED--------------\n")
            self.checkForDownload()
        })
RunLoop.main.add(self.timer!, forMode: RunLoopMode.defaultRunLoopMode)


来源:https://stackoverflow.com/questions/4773559/iphone-nstimer-not-repeating-after-fire

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!