2017-01-10 156 views
2

我打電話的setValue()的背景是這樣的:如何在後臺爲Firebase實時數據庫設置值?

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) 
} 

但完成塊永遠不會被調用。在後臺上傳數據是不可能的嗎?

+0

得到了同樣的問題。 POST請求到其他服務器在後臺工作,但Firebase什麼都不做 –

回答

0

蘋果允許一些功能在後臺被稱爲

  1. 麥克風
  2. 位置更新
  3. 藍牙配件
  4. 的VoIP
+0

但是我們可以從後臺獲取數據庫中的數據。 –

0

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) 
相關問題