ios - 当我模拟后台提取时,我收到一条警告,说从未调用完成处理程序

标签 ios swift 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 是否正常工作?

最佳答案

如果您不输入第一个 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/

相关文章:

ios - 警告 : Application delegate received call to -application:performFetchWithCompletionHandler: but the completion handler was never called

ios - 快速时区转换问题

c++ - Firebase:可以混合使用 C++ 和 Objective-C SDK 吗?

ios - 具有自定义 TableViewCell 的随机信号 Sigabrt

swift - apple-app-site-association 文件是否适用于 GitHub Pages(即 site.github.io)?

ios - 创建一个包含 UIAlertView、UIActivityIndi​​cator 等函数的类,并在各种 viewController 中回调它们

ios - 后台获取和核心位置与时间表

ios - PDF 文件阅读问题仅在离线模式下 swift

ios - CV日历: How to override CVCalendar properties that do not have explicit function API?

iOS 后台获取运行时间过长?