Azi*_*ode 5 ios swift background-fetch
我按照所有步骤进行设置,background fetch但我怀疑performFetchWithCompletionHandler在 AppDelegate 中编写函数时犯了一个错误。
这是我在模拟后立即收到的警告 background fetch
Warning: Application delegate received call to - application:
performFetchWithCompletionHandler:but the completion handler was never called.
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
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")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我如何测试它background-fetch是否有效?
如果您不输入第一个 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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7935 次 |
| 最近记录: |