我得到一个警告,说我在模拟后台提取时从未调用完成处理程序

时间:2016-02-25 23:57:07

标签: ios swift background-fetch

我按照所有步骤设置了$?,但我怀疑在AppDelegate中编写函数xterm时犯了一个错误。

以下是我模拟background fetch

时收到的警告
performFetchWithCompletionHandler

这是我的代码:

background fetch

如何测试Warning: Application delegate received call to - application: performFetchWithCompletionHandler:but the completion handler was never called. 是否有效?

1 个答案:

答案 0 :(得分:3)

如果未输入第一个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)
}