Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在后台设置Firebase实时数据库的值?_Ios_Swift_Firebase_Firebase Realtime Database - Fatal编程技术网

Ios 如何在后台设置Firebase实时数据库的值?

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

我在后台调用setValue(),如下所示:

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建议,但没有成功。