我是iOS / Swift的新手。我在stackoverflow.com上尝试了很多Q& As但还没找到解决方案。
有没有人解释我为什么我的计时器第一次不工作(*),但第二次工作正常。
(*)第一次:将应用程序运行到我的iPhone(5S),在应用程序完成启动后,单击Home转到后台,在intervalTime之后, startSendDataSchedule 被调用但不完整且后台结束。应用程序停在这里 几秒钟后,我将应用程序前移, startSendDataSchedule 再次运行并将数据发送到服务器,打印出许多日志。
第二次:从(*)再次单击“主页”转到后台, startSendDataSchedule 继续运行。我断开服务器以退出startSendDataSchedule。调用Ater intervalTime, startSendDataSchedule 并按照我的期望行事。从现在开始,Timer工作正常
我的错误是什么?如果有人回答我,我将不胜感激。
我的代码(Swift 3,xcode 8.2 iOS 10.2):
static func startFetching(inttervalTime : TimeInterval){
registerBackgroundTask() // register background with UIApplication.shared.beginBackgroundTask
//inttervalTime = 5 min
let date = NSDate().addingTimeInterval(inttervalTime)
print("Background trace1: thread=\(Thread.current)")
if (Thread.isMainThread == true) {
print("Background trace: main thread") // Go to Here
} else {
print("Background trace: thread=\(Thread.current)")
}
guard timerT == nil else { return }
// Set scheduler
//DispatchQueue.main.async {
self.timerT = Timer(fireAt: date as Date, interval: inttervalTime, target: self, selector: #selector(startSendDataSchedule), userInfo: nil, repeats: true)
//self.timerT = Timer.scheduledTimer(timeInterval: inttervalTime, target: self, selector: #selector(startSendDataSchedule), userInfo: nil, repeats: true)
//
RunLoop.main.add(timerT!, forMode: RunLoopMode.commonModes) // need if you use Timer init, no if use scheduledTimer
//}
// Subscribe to UIApplicationDidBecomeActive
NotificationCenter.default.addObserver(self, selector: #selector(reinstateBackgroundTask), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
}
答案 0 :(得分:0)
我通过将[日期作为日期]更改为[日期()]
立即解决了火灾问题self.timerT = Timer( fireAt:Date(),interval:inttervalTime,target:self,selector:#selector(startSendDataSchedule),userInfo:nil,repeats:true)
如果我的iPHone连接到XCode,它可以正常工作。但是当直接运行iPhone时,Timer没有被解雇,我发现我的应用程序崩溃了。
我的应用名称:HealthSprite
崩溃日志: ncident标识符:AFE9BD21-F6B3-473F-96E7-60778FF26647 CrashReporter密钥:dec5755cdf37668f857c5e737e56f147e768c673 硬件型号:iPhone6,1 流程:HealthSprite [598] 路径:/private/var/containers/Bundle/Application/3C5942E3-4308-489A-94BB-DA5DEFB48B39/HealthSprite.app/HealthSprite 标识符:jp.sprite.vn.HealthSprite 版本:7526(0.9.0) 代码类型:ARM-64(Native) 作用:前景 父流程:launchd [1] 联盟:jp.sprite.vn.HealthSprite [731]
日期/时间:2017-04-12 15:21:13.3154 +0700 发布时间:2017-04-12 15:18:01.0932 +0700 操作系统版本:iPhone OS 10.2.1(14D27) 报告版本:104
异常类型:EXC_CRASH(SIGKILL) 例外代码:0x0000000000000000,0x0000000000000000 例外注意:EXC_CORPSE_NOTIFY 终止原因:命名空间SPRINGBOARD,代码0x8badf00d 由线程触发:0
过滤后的系统日志: 找不到
线程0名称:Dispatch queue:com.apple.main-thread 线程0崩溃: 0 libsystem_kernel.dylib 0x0000000186299188 mach_msg_trap + 8
1 libsystem_kernel.dylib 0x0000000186298ff8 mach_msg + 72
2 CoreFoundation 0x00000001872965d0 __CFRunLoopServiceMachPort + 192
3 CoreFoundation 0x00000001872941ec __CFRunLoopRun + 1132
4 CoreFoundation 0x00000001871c22b8 CFRunLoopRunSpecific + 444
5 GraphicsServices 0x0000000188c76198 GSEventRunModal + 180
6 UIKit 0x000000018d2097fc - [UIApplication _run] + 684
7 UIKit 0x000000018d204534 UIApplicationMain + 208
8 HealthSprite 0x00000001000f8ffc 0x100078000 + 528380
9 libdyld.dylib 0x00000001861a55b8 start + 4
...