Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase runTransactionBlock not working in swift 3

After updating the Xcode 8 and convert the Swift 2 project into Swift 3, other codes work well, but runTransactionBlock is not working and found this error:

"runTransactionBlock: usage detected while persistence is enabled. Please be aware that transactions will not be persisted across app restarts"

What could be wrong?

Firebase runTransaction Code

postRef.runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in
            if var post = currentData.value as? [String : AnyObject], let uid = FIRAuth.auth()?.currentUser?.uid {
                var stars : Dictionary<String, Bool>
                stars = post["likeMap"] as? [String : Bool] ?? [:]
                var likeCount = post["likeCount"] as? Int ?? 0
                if let _ = stars[uid] {
                    // Unstar the post and remove self from stars
                    likeCount -= 1
                    self._liked = false
                    stars.removeValue(forKey: uid)
                } else {
                    // Star the post and add self to stars
                    likeCount += 1
                    self._liked = true
                    stars[uid] = true
                }
                post["likeCount"] = likeCount as AnyObject?
                post["likeMap"] = stars as AnyObject?
                self._likeCount = likeCount
                // Set value and report transaction success
                currentData.value = post

                return FIRTransactionResult.success(withValue: currentData)
            }
            return FIRTransactionResult.success(withValue: currentData)
        }) { (error, committed, snapshot) in
            if let error = error {
                print(error.localizedDescription)
            }
        }
like image 860
Jelly Avatar asked Sep 29 '16 06:09

Jelly


2 Answers

Not sure which version of Firebase SDK you are using but I will provide you a basic example I used runTransactionBlock successfully:

Database

enter image description here


Code

let ref = FIRDatabase.database().reference()
let refReservations = ref.child("reservations")


refReservations.runTransactionBlock { (currentData: FIRMutableData) -> FIRTransactionResult in
  if var data = currentData.value as? [String: Any] {
    var count = data["count"] as? Int ?? 0
    count += 1
    data["count"] = count
    
    currentData.value = data
  }
  
  return FIRTransactionResult.success(withValue: currentData)
}

Note

I've used Firebase SDK version : 3.0.1 and you can get this info from your code using this method: FIRDatabase.sdkVersion()

like image 90
Wilson Avatar answered Sep 19 '22 12:09

Wilson


That message is a warning that your app is using transactions and is persisting data to disk.

When you enable disk persistence, the (recent) data that your app observes is persisted to disk. That way the data will be available in case the user restarts the app at a moment when there is no network connection.

In this disconnected state, the Firebase client will also store all local write operations to disk in a so-called pending writes queue. Then, when a network connection becomes available again, the Firebase client will send all pending writes to the server, and of course also receive any updates that the server has for it.

But, while disconnected, the Firebase client will not persist transactions to disk. Since that combination may not be what you expected, Firebase logs the warning about it that you see.

like image 26
Frank van Puffelen Avatar answered Sep 18 '22 12:09

Frank van Puffelen