根据教程-> https://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial3,我无法在swift 2.0上使用后台获取。
我收到此错误:
application:performFetchWithCompletionHandler:但从未调用完成处理程序。
基本上我有一个函数来执行我的动作(在Firebase上调用数据),并且希望它在后台执行。
这是我的应用程序委托代码
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(
UIApplicationBackgroundFetchIntervalMinimum)
}
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
if let tabBarController = window?.rootViewController as? UITabBarController,
viewControllers = tabBarController.viewControllers! as [UIViewController]!
{
for viewController in viewControllers {
if let a1 = viewController as? HorariosViewController {
completionHandler(.NewData)
a1.interface()
}
}
}
}
这是我如何从Firebase上的接口函数获取数据:
func interface() {
self.numeroDasOrações = []
self.adhan = []
if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {
for snap in snapshots {
if let postDictionary = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let hSalat = Horarios(key: key, dictionary: postDictionary)
let hAdhan = Horarios(key: key, dictionary: postDictionary)
self.numeroDasOrações.append(hSalat)
self.adhan.append(hAdhan)
}
}
}
})
}
Xcode错误:
警告:应用程序委托收到了对-application:performFetchWithCompletionHandler:的调用,但从未调用完成处理程序。
提前致谢。
最佳答案
使用application(_:didReceiveRemoteNotification:)
时,无论如何都必须始终调用完成处理程序。苹果公司的政策是,如果您的抓取找到了新数据,则调用completionHandler(.newData)
;如果您的抓取未找到任何新数据,则调用completionHandler(.noData)
;如果您的抓取找到了新数据,则调用completionHandler(.failed)
,但在尝试检索新数据时失败。
在您的代码中,仅在满足某些条件时才调用完成处理程序。而不是不调用完成处理程序,您应该调用completionHandler(.failed)
或completionHandler(.noData)
。
因此,您的最终代码(为Swift 3更新)将是:
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
var newData = false
if let tabBarController = window?.rootViewController as? UITabBarController,
viewControllers = tabBarController.viewControllers! as [UIViewController]!
{
for viewController in viewControllers {
if let a1 = viewController as? HorariosViewController {
newData = true
a1.interface()
}
}
}
completionHandler(newData ? .newData : .failed) // OR completionHandler(newData ? .newData : .noData)
}
关于ios - iOS Firebase后台获取,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36592137/