Ios 如何在后台设置Firebase实时数据库的值?
我在后台调用setValue(),如下所示:Ios 如何在后台设置Firebase实时数据库的值?,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,我在后台调用setValue(),如下所示: func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { let semaphore: Dispatch
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let semaphore: DispatchSemaphore = DispatchSemaphore(value: 0)
let ref = FIRDatabase.database().reference().child("data")
let data: NSDictionary = [
"test" : "test"
]
ref.setValue(data, withCompletionBlock:{
(error, reference) in
if error != nil {
print(error!)
} else {
print("success")
semaphore.signal()
}
})
_ = semaphore.wait(timeout: .distantFuture)
completionHandler(.newData)
}
但我从未打过电话。在后台上传数据是不可能的吗?苹果允许在后台调用一些功能
keepSynced(true)
为我做了这个把戏,只需添加到您的代码中即可
let semaphore: DispatchSemaphore = DispatchSemaphore(value: 0)
let ref = FIRDatabase.database().reference().child("data")
let data: NSDictionary = [
"test" : "test"
]
ref.keepSynced(true)
ref.setValue(data, withCompletionBlock:{
(error, reference) in
if error != nil {
print(error!)
} else {
print("success")
semaphore.signal()
}
})
_ = semaphore.wait(timeout: .distantFuture)
completionHandler(.newData)
但我们可以在后台从数据库中获取数据。遇到了同样的问题。对其他服务器的POST请求在后台工作,但Firebase对此没有任何更新@Shangri La?我尝试了@DimaRostopira建议,但没有成功。