我按照所有步骤来设置后台获取
,但我怀疑我在 AppDelegate 中编写函数 performFetchWithCompletionHandler
时犯了一个错误。
这是我在模拟 background fetch
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 语句,则永远不会调用完成处理程序。此外,当您遍历 View Controller 时,您可能找不到您正在寻找的 View Controller ,这意味着永远不会调用完成。最后,您可能应该在调用完成处理程序后放置一个return
。
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)
}
关于ios - 当我模拟后台提取时,我收到一条警告,说从未调用完成处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35640788/