IOS Firebase 后台获取
IOS Firebase background fetch
根据教程 -> 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 的调用:但从未调用完成处理程序。
提前致谢。
使用 application(_:didReceiveRemoteNotification:)
时,无论如何都必须始终调用完成处理程序。 Apple 的政策是,如果您的提取找到新数据,您调用 completionHandler(.newData)
,如果您的提取没有找到任何新数据,则调用 completionHandler(.noData)
,如果您的提取找到新数据,则调用 completionHandler(.failed)
,但在试图找回它。
在您的代码中,仅当满足特定条件时才会调用完成处理程序。您应该调用 completionHandler(.failed)
或 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)
}
根据教程 -> 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 的调用:但从未调用完成处理程序。
提前致谢。
使用 application(_:didReceiveRemoteNotification:)
时,无论如何都必须始终调用完成处理程序。 Apple 的政策是,如果您的提取找到新数据,您调用 completionHandler(.newData)
,如果您的提取没有找到任何新数据,则调用 completionHandler(.noData)
,如果您的提取找到新数据,则调用 completionHandler(.failed)
,但在试图找回它。
在您的代码中,仅当满足特定条件时才会调用完成处理程序。您应该调用 completionHandler(.failed)
或 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)
}