Timer that updates label running in background

我与影子孤独终老i 提交于 2019-12-04 04:56:14

问题


I have coded a timer using an NSTimer that updates a label. The problem is that in the same viewcontroller I have a uitableview and when I scroll it down, the timer doesn't update its value so the user can "cheat" to stop the timer.

I think this could be easy to fix with a serial queue with CGD but I don't figure out how to do it.

Thanks in advance,

Juan


回答1:


First of all keep in mind that you cannot perform UI changes in any other thread than the main thread. Having said that, you need the NSTimer to fire in the main queue, otherwise the program will crash when changing the UILabel. Have a look at this links http://bynomial.com/blog/?p=67 and this one http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.html

To my knowledge, if you schedule the timer in the for the NSRunLoopCommonModes it will ignore event updates and fire the timer jsut how you want it:

 NSTimer *timer = [NSTimer timerWithTimeInterval:1.0
                                           target:self
                                         selector:@selector(timerDidTick:)
                                         userInfo:nil repeats:YES];
  [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

-(void) timerDidTick:(NSTimer*) theTimer{
    [[self myLabel] setText:@"Timer ticked!"];
}



回答2:


I have faced same problem if u run timer in main thread than when you scroll tableview its stop timer too. Solution is that you run timer in background and update GUI in main thread

    UIApplication *app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //run function methodRunAfterBackground
    updateTimer1=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateGUIAudioPlayer) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:updateTimer1 forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
});

    -(void)updateGUIAudioPlayer {
dispatch_async(dispatch_get_main_queue(), ^{
    self.label.text = @"your text";
});

    //but if you want to stop timer by himself than use main thread too like that

    dispatch_async(dispatch_get_main_queue(), ^{
             [updateTimer1 invalidate];
         });}



回答3:


-(void) timerDidTick:(NSTimer*) theTimer{
        _currentNumber =_currentNumber+1;
        //check if current number is bigger than the contents of the word array
        if (_currentNumber <=wordArray.count-1) {
            NSLog(@"_currentNumber @%i wordArray @%i",_currentNumber,wordArray.count);
            _RandomLoadingText.text = @"Timer" ;
            [self changeTextInRandomLoadingLabel:_currentNumber];
        }

        else if (_currentNumber >=wordArray.count-1)
         {
             NSLog(@"reached end of array");
             [timer invalidate];
         }
    }

-(void)changeTextInRandomLoadingLabel:(int)myInt
{
    _RandomLoadingText.text = [wordArray objectAtIndex:myInt];
}

//--------- view did load

    _RandomLoadingText.text = @"Test";
    _currentNumber =0;
    timer = [NSTimer timerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(timerDidTick:)
                                           userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];



回答4:


To update the UI you need to use OperationQueue.main. Supposing you have a UILabelnamed timeLabel:

var timeInSeconds = 0

timer = Timer(timeInterval: 1, repeats: true, block: { timer in
        timeInSeconds += 1
        OperationQueue.main.addOperation {
            self.timeLabel.text = String(timeInSeconds)
        }
    })

RunLoop.main.add(timer, forMode: .commonModes)


来源:https://stackoverflow.com/questions/16261620/timer-that-updates-label-running-in-background

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