我得到一个警告,说我在模拟后台提取时从未调用完成处理程序 [英] I get a warning saying that the completion handler was never called When I simulate a Background fetch
问题描述
我按照所有步骤设置后台提取
但是我怀疑在编写函数 performFetchWithCompletionHandler时我犯了一个错误
在AppDelegate中。
I followed all the steps in order to set up the background fetch
but I'm suspecting that I made a mistake when writing the function performFetchWithCompletionHandler
in the AppDelegate.
这是我在模拟后台提取时得到的警告
Warning: Application delegate received call to - application:
performFetchWithCompletionHandler:but the completion handler was never called.
这是我的代码:
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 notificationViewController = viewController as? NotificationsViewController {
firstViewController.reloadData()
completionHandler(.NewData)
print("background fetch done")
}
}
}
}
如何测试 background-fetch
正在运作?
推荐答案
如果不输入第一个if语句,则永远不会调用完成处理程序。此外,当您循环浏览视图控制器时,您可能找不到正在查找的视图控制器,这意味着永远不会调用完成。最后,你应该在调用完成处理程序后输入 return
。
If you don't enter the first if statement, the completion handler will never be called. Also, you could potentially not find the view controller you're looking for when you loop through the view controllers, which would mean the completion would never be called. Lastly, you should probably put a return
after you call the completion handler.
func application(
application: UIApplication,
performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
) {
guard let tabBarController = window?.rootViewController as? UITabBarController,
let viewControllers = tabBarController.viewControllers else {
completionHandler(.failed)
return
}
guard let notificationsViewController = viewControllers.first(where: { $0 is NotificationsViewController }) as? NotificationsViewController else {
completionHandler(.failed)
return
}
notificationViewController.reloadData()
completionHandler(.newData)
}
这篇关于我得到一个警告,说我在模拟后台提取时从未调用完成处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!