5

をセットアップするためにすべての手順に従いましたが、AppDelegate でbackground fetch関数を作成するときに間違いを犯したのではないかと疑っています。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ですか?

4

1 に答える 1

3

最初の if ステートメントを入力しないと、完了ハンドラーは呼び出されません。また、ビュー コントローラーをループするときに、探しているビュー コントローラーが見つからない可能性があります。これは、完了が呼び出されないことを意味します。最後に、return完了ハンドラを呼び出した後に a を配置する必要があります。

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)
}
于 2016-02-25T23:59:54.343 に答える