IOS Firebase background fetch

醉酒当歌 提交于 2019-12-07 20:05:10

问题


I'm having trouble using background fetch on swift 2.0 according to the tutorial -> https://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial3. I get this error: application:performFetchWithCompletionHandler: but the completion handler was never called.

Basically i have a function where i perform my actions (calling data on firebase) and i want it to execute on background.

Here is my app delegate code

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()   
            }
        }
    }
}

here is how i get data from firebase on the interface function :

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 error :

Warning: Application delegate received call to -application:performFetchWithCompletionHandler: but the completion handler was never called.

Thanks in advance.


回答1:


When using application(_:didReceiveRemoteNotification:) you must always call the completion handler, no matter what. Apples policy is that you call completionHandler(.newData) if your fetch found new data, completionHandler(.noData) if your fetch didn't find any new data, and completionHandler(.failed) if your fetch found new data, but failed while trying to retrieve it.

In your code the completion handler is only called if certain conditions are met. Instead of not calling the completion handler, you should call completionHandler(.failed) or completionHandler(.noData).

Thus, your final code (updated for Swift 3) will be:

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)
}


来源:https://stackoverflow.com/questions/36592137/ios-firebase-background-fetch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!