首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我模拟后台抓取时,我收到一个警告,说完成处理程序从未被调用过

当我模拟后台抓取时,我收到一个警告,说完成处理程序从未被调用过
EN

Stack Overflow用户
提问于 2016-02-26 07:57:07
回答 1查看 8K关注 0票数 5

为了设置background fetch,我遵循了所有步骤,但我怀疑在AppDelegate中编写函数performFetchWithCompletionHandler时犯了错误。

以下是我在模拟background fetch时立即收到的警告

代码语言:javascript
复制
Warning: Application delegate received call to -  application:
performFetchWithCompletionHandler:but the completion handler was never called.

下面是我的代码:

代码语言:javascript
复制
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是否正常工作?

EN

回答 1

Stack Overflow用户

发布于 2016-02-26 07:59:54

如果不输入第一个If语句,则永远不会调用完成处理程序。此外,当您循环遍历视图控制器时,可能找不到您正在寻找的视图控制器,这意味着永远不会调用完成。最后,您可能应该在调用完成处理程序之后放置一个return

代码语言:javascript
复制
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)
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35640788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档