nstimer

Running a Timer in the background

好久不见. 提交于 2021-02-15 06:48:31
问题 I'm writing an app in Swift where a timer counts down, much like a countdown clock. To do this I am using this code in my main logics class: func start() { self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in self.run() } } Now every time I close the app, the timer stops and I get this error: BackgroundTask: no background task exists with identifier 1 (0x1), or it may have already been ended. Is there any way to continue the timer running in the background? Or do

Running a Timer in the background

邮差的信 提交于 2021-02-15 06:48:27
问题 I'm writing an app in Swift where a timer counts down, much like a countdown clock. To do this I am using this code in my main logics class: func start() { self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in self.run() } } Now every time I close the app, the timer stops and I get this error: BackgroundTask: no background task exists with identifier 1 (0x1), or it may have already been ended. Is there any way to continue the timer running in the background? Or do

SwiftUI - Optional Timer, reset and recreate

左心房为你撑大大i 提交于 2021-02-08 08:21:29
问题 Normally, I would use an optional variable to hold my Timer reference, as it's nice to be able to invalidate and set it to nil before recreating. I'm trying to use SwiftUI and want to make sure I'm correctly doing so... I declare as: @State var timer:Publishers.Autoconnect<Timer.TimerPublisher>? = nil Later I: self.timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() To drive a UI text control I use: .onReceive(timer) { time in print("The time is now \(time)") } What is the

Invalidate Timer from Function in Swift

早过忘川 提交于 2021-02-05 12:19:17
问题 I have an app written in Swift with an NSTimer declared in the viewDidLoad ; the timer runs a function once every second. Here's the code inside my viewDidLoad() : let checkStateTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "callCheckState:", userInfo: nil, repeats: true) Currently, I have another function which is called which I want to pause the timer. It should, I believe, use: checkStateTimer.invalidate() However, because the timer is in the viewDidLoad and

Invalidate Timer from Function in Swift

时光总嘲笑我的痴心妄想 提交于 2021-02-05 12:17:26
问题 I have an app written in Swift with an NSTimer declared in the viewDidLoad ; the timer runs a function once every second. Here's the code inside my viewDidLoad() : let checkStateTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "callCheckState:", userInfo: nil, repeats: true) Currently, I have another function which is called which I want to pause the timer. It should, I believe, use: checkStateTimer.invalidate() However, because the timer is in the viewDidLoad and

What happed when we excute NSTimer invalidate?

微笑、不失礼 提交于 2021-01-29 09:30:43
问题 self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timeCall:) userInfo:self.strong repeats:YES]; [self.timer invalidate]; NSLog(@"%p",[self.timer userInfo]); Then app crashes because EXC_BAD_ACCESS . Apple document says, This method is the only way to remove a timer from an NSRunLoop object. The NSRunLoop object removes its strong reference to the timer, either just before the invalidate method returns or at some later point. If it was configured with

【转】NSTimer

ε祈祈猫儿з 提交于 2020-12-01 01:57:13
转自:http://www.cnblogs.com/mgen/p/3276722.html 1. NSRunLoopCommonModes和Timer 当使用NSTimer的scheduledTimerWithTimeInterval方法时。事实上此时Timer会被加入到当前线程的Run Loop中,且模式是默认的NSDefaultRunLoopMode。而如果当前线程就是主线程,也就是UI线程时,某些UI事件,比如UIScrollView的拖动操作,会将Run Loop切换成NSEventTrackingRunLoopMode模式,在这个过程中,默认的NSDefaultRunLoopMode模式中注册的事件是不会被执行的。也就是说,此时使用scheduledTimerWithTimeInterval添加到Run Loop中的Timer就不会执行。 所以为了设置一个不被UI干扰的Timer,我们需要手动创建一个Timer,然后使用NSRunLoop的addTimer:forMode:方法来把Timer按照指定模式加入到Run Loop中。这里使用的模式是:NSRunLoopCommonModes,这个模式等效于NSDefaultRunLoopMode和NSEventTrackingRunLoopMode的结合。(参考 Apple文档 ) 参考代码: - ( void

NSTimer -滑动中的NSTimer

不问归期 提交于 2020-12-01 01:28:27
个人博客地址 NSTimer误差原因 (NSDefaultRunLoopMode 是普通状态下,TrackingRunLoopMode是处于滑动状态) 1,NSTimer添加在主线程中,模式是NSDefaultRunLoopMode, 主线程处理所有添加在主线程中的事件,例如UI界面的刷新,复杂的运算,等等,过多主线程事件的处理,导致线程阻塞。 2,模式的改变,当NSTimer添加到NSDefaultRunLoopMode中的时候,会重复调用,当滑动ScrollView的时候,Runlop会将Model切换到TrackingRunLoopMode,这时候的NSTimer事件就不会回调,所以不准。 <!more-> @interface ViewController () /** 定时器1 */ @property (nonatomic,strong)NSTimer *timer; @end - (void)viewDidLoad { [super viewDidLoad]; // 主线程 [self mainThreadTimerOne]; [self mainThreadTimerTwo]; // 子线程 NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector

Timer in another thread in Objective - C

喜夏-厌秋 提交于 2020-08-21 06:41:13
问题 I have to define method which should be invoked periodically with some time interval. I need to invoke it in another thread (NOT main thread), because this method is used to get information from external API and sync data in core data. How do I define this method to not block main thread? 回答1: Unless you have a specific need for timers, you can use Grand Central Dispatch. The following snippet will execute a block after 2 seconds, on the default priority concurrent queue (i.e a background