2

根据教程-> https://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial3 ,我在 swift 2.0 上使用后台获取时遇到问题。我收到此错误: application:performFetchWithCompletionHandler: 但从未调用过完成处理程序。

基本上我有一个功能,我可以在其中执行我的操作(在 firebase 上调用数据),我希望它在后台执行。

这是我的应用委托代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(
    UIApplicationBackgroundFetchIntervalMinimum)
}


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 a1 = viewController as? HorariosViewController {
              completionHandler(.NewData)
              a1.interface()   
            }
        }
    }
}

这是我在接口函数上从firebase获取数据的方式:

func interface() {

                self.numeroDasOrações = []
                self.adhan = []

                if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {

                    for snap in snapshots {
                        if let postDictionary = snap.value as? Dictionary<String, AnyObject> {
                            let key = snap.key
                            let hSalat = Horarios(key: key, dictionary: postDictionary)
                            let hAdhan = Horarios(key: key, dictionary: postDictionary)

                            self.numeroDasOrações.append(hSalat)
                            self.adhan.append(hAdhan)

                        }
                    }
                }
            })
}

Xcode 错误:

警告:应用程序委托收到了对 -application:performFetchWithCompletionHandler: 的调用,但从未调用过完成处理程序。

提前致谢。

4

1 回答 1

1

使用时application(_:didReceiveRemoteNotification:),无论如何都必须始终调用完成处理程序。Apple 的政策是,completionHandler(.newData)如果您的 fetch 找到了新数据,completionHandler(.noData)如果您的 fetch 没有找到任何新数据,并且completionHandler(.failed)您的 fetch 找到了新数据,但在尝试检索它时失败了,您就会调用。

在您的代码中,仅在满足某些条件时才调用完成处理程序。您应该调用completionHandler(.failed)or ,而不是不调用完成处理程序completionHandler(.noData)

因此,您的最终代码(针对 Swift 3 更新)将是:

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    var newData = false
    if let tabBarController = window?.rootViewController as? UITabBarController,
        viewControllers = tabBarController.viewControllers! as [UIViewController]!
    {
        for viewController in viewControllers {
            if let a1 = viewController as? HorariosViewController {
                newData = true
                a1.interface()   
            }
        }
    }
    completionHandler(newData ? .newData : .failed) // OR completionHandler(newData ? .newData : .noData)
}
于 2016-09-24T21:16:35.743 回答