问题描述
根据教程,我在swift 2.0上使用背景获取时遇到问题-> https://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial3 .我收到此错误:application:performFetchWithCompletionHandler:但从未调用完成处理程序.
I'm having trouble using background fetch on swift 2.0 according to the tutorial -> https://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial3.I get this error:application:performFetchWithCompletionHandler: but the completion handler was never called.
基本上,我有一个函数来执行我的动作(在Firebase上调用数据),并且希望它在后台执行.
Basically i have a function where i perform my actions (calling data on firebase) and i want it to execute on background.
这是我的应用程序委托代码
Here is my app delegate code
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获取数据的方式:
here is how i get data from firebase on the interface function :
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的调用:但从未调用完成处理程序.
Warning: Application delegate received call to -application:performFetchWithCompletionHandler: but the completion handler was never called.
谢谢.
推荐答案
使用 application(_:didReceiveRemoteNotification:)
时,无论如何都必须始终调用完成处理程序.苹果公司的政策是,如果您的抓取找到了新数据,则调用 completionHandler(.newData)
,如果您的抓取未找到任何新数据,则调用 completionHandler(.noData)
,并 completionHandler(.failed)
,如果您的抓取找到了新数据,但是在尝试检索新数据时失败.
When using application(_:didReceiveRemoteNotification:)
you must always call the completion handler, no matter what. Apples policy is that you call completionHandler(.newData)
if your fetch found new data, completionHandler(.noData)
if your fetch didn't find any new data, and completionHandler(.failed)
if your fetch found new data, but failed while trying to retrieve it.
在您的代码中,仅在满足某些条件时才调用完成处理程序.而不是不调用完成处理程序,您应该调用 completionHandler(.failed)
或 completionHandler(.noData)
.
In your code the completion handler is only called if certain conditions are met. Instead of not calling the completion handler, you should call completionHandler(.failed)
or completionHandler(.noData)
.
因此,您的最终代码(为Swift 3更新)将为:
Thus, your final code (updated for Swift 3) will be:
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 Firebase后台获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!