IOS Firebase后台获取
根据教程->,我在swift 2.0上使用后台获取时遇到问题。 我得到这个错误: application:performFetchWithCompletionHandler:但从未调用完成处理程序 基本上,我有一个函数,可以执行我的操作(在firebase上调用数据),并希望它在后台执行 这是我的应用程序代理代码IOS Firebase后台获取,ios,swift,firebase,background-fetch,Ios,Swift,Firebase,Background Fetch,根据教程->,我在swift 2.0上使用后台获取时遇到问题。 我得到这个错误: application:performFetchWithCompletionHandler:但从未调用完成处理程序 基本上,我有一个函数,可以执行我的操作(在firebase上调用数据),并希望它在后台执行 这是我的应用程序代理代码 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [N
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)
}
}
}
})
}
func接口(){
self.numeriodasoraões=[]
self.adhan=[]
如果让快照=snapshot.children.allObjects为?[FDataSnapshot]{
用于管理单元快照{
如果让postDictionary=snap.value作为字典{
让key=snap.key
设hSalat=Horarios(键:键,字典:postDictionary)
让hAdhan=Horarios(key:key,dictionary:postdirectionary)
self.numeriodasoraões.append(hSalat)
self.adhan.append(hAdhan)
}
}
}
})
}
Xcode错误:
警告:应用程序委托收到对-Application:performFetchWithCompletionHandler:的调用,但从未调用完成处理程序
提前感谢。在使用
应用程序(\uuuIdReceiveMemotentification:)
时,无论发生什么情况,都必须始终调用完成处理程序。苹果公司的策略是,如果您的获取找到了新数据,则调用completionHandler(.newData)
;如果您的获取没有找到任何新数据,则调用completionHandler(.noData)
;如果您的获取找到了新数据,但在尝试检索时失败,则调用completionHandler(.failed)
在代码中,仅当满足某些条件时才会调用完成处理程序。与其不调用完成处理程序,不如调用completionHandler(.failed)
或completionHandler(.noData)
因此,您的最终代码(为Swift 3更新)将为:
请出示您的密码。。。来自XCode的确切错误/警告消息…@kishan94我刚刚编辑了我的问题。我认为completionHandler调用应该是completionHandler(UIBackgroundFetchResult.NewData)。您的代码中似乎没有Firebase观察或查询函数,因此不清楚快照来自何处。作为后台进程运行异步代码可能会导致其他问题,但这取决于用例是什么。
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)
}