我有一种情况,我正在使用后台获取来调用我的数据同步过程,因为同步功能是一项繁重的任务,它在后台线程中执行。
这是我的代码,
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("Background Fetch")
Utilities.syncCompleted = false // declared as :> static var syncCompleted:Bool = false
BackgroundSync().startSync() // heavy background task, and iam updating [Utilities.syncCompleted = true) on thread completion
while Utilities.syncCompleted == false {
NSThread.sleepForTimeInterval(1) // sleep for sometime
}
if Utilities.syncCompleted{
completionHandler(UIBackgroundFetchResult.NewData)
}else {
completionHandler(UIBackgroundFetchResult.NoData)
}
}
现在我有一些问题:
由于后台获取时间为 30 秒,如果我的任务未在 30 秒内完成,那么会发生什么,因为我无法将 completionHandler 设置为 .NoData 或 .Failure
如果开发人员未在 30 秒内指定,是否设置了任何默认的 completionHandler 值(如 .NoData)。
有没有其他更好的方法来做到这一点。
提前致谢