-->

Firebase database transaction when app is in backg

2020-02-14 08:16发布

问题:

I am working with interactive push notifications, when i reply to a PN, which should be updating firbase databsae. It doesn't works well because some times if the app is in background the data is not synced in real time. but when u open the app it pushes the data

if the app is in a killed state the reply is not even send to Firebase DB

Ref.child(uId).runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in
var value = currentData.value as? [String : AnyObject]
           
           if value == nil {
                return FIRTransactionResult.success(withValue: currentData)
           }

           let totR = value!["totalReply"] as? Int ?? 0
           print(totR)
           value?["totalReply"] =  totR + 1

           currentData.value = value
}

回答1:

Firebase Database does not provide background syncing. Instead, it syncs data in realtime while the app is actively running. For background syncing you could use Firebase Cloud Messaging to wake the app up so that Database can then synchronize its data.