我很难completionHandler
在performFetchWithCompletionHandler
.AppDelegate
我的应用程序进行了一些后台提取,并且在提取期间我想调用位于我的一个 viewControllers 中的函数
使用 的正确方法是什么completionHandler
,我知道它是一个异步任务。
当我想将数据发送到我的数据库时,我设法让它工作,但是当我想调用位于其中一个 viewControllers 中的函数时,我无法让它工作
尝试:正确的调用方式是completionHandler
什么?
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 myViewController = viewController as? MyViewController {
myViewController.myFunction()
print("background fetch done")
completionHandler(.NewData)
print("background fetch done")
}
}
}
}
PS:这是成功的尝试,这是我将数据发送到我的数据库时:
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
let name = UIDevice.currentDevice().name
let val = "\(name): \(timeFormatter.stringFromDate(NSDate()))"
db.childByAutoId().setValue(val) { _ in
print("Background Fetch Completed")
completionHandler(.NewData)
}
}