为了设置background fetch,我遵循了所有步骤,但我怀疑在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是否正常工作?
发布于 2016-02-26 07:59:54
如果不输入第一个If语句,则永远不会调用完成处理程序。此外,当您循环遍历视图控制器时,可能找不到您正在寻找的视图控制器,这意味着永远不会调用完成。最后,您可能应该在调用完成处理程序之后放置一个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)
}https://stackoverflow.com/questions/35640788
复制相似问题