Firebase runTransactionBlock not working in swift 3

After upgrading Xcode 8 and converting Swift 2 to Swift 3, other codes work fine, but it runTransactionBlockdoesn't work and found this error:

"runTransactionBlock: use detected when persistence persists. Note that transactions will not persist through application reloads"

What could be wrong?

RunTransaction Firebase 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)
            }
        }
+4
source share
3 answers

You do not know which version of the Firebase SDK you are using, but I will give you a basic example. I have successfully used runTransactionBlock :

Database

enter image description here


The 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

SDK Firebase: 3.0.1, , : FIRDatabase.sdkVersion()

+5

, .

(), , . , , , .

Firebase . , , Firebase , , , .

, , Firebase . , , Firebase , .

+2

, .

Firebase Swift 3, , FIRMutableData, , [String: AnyObject], [String: Any].

The reason is that the answer is actually [String: Dictionary], and the Dictionary is not AnyObject (anyone can represent an instance of any type whatsoever, including function types and optional types. AnyObject can represent an instance of any type of class.) Because the Dictionary is not a class .

For this code to work, you must replace this line of code:

if var post = currentData.value as? [String : AnyObject], let uid = FIRAuth.auth()?.currentUser?.uid {

with this line:

if var post = currentData.value as? [String : Any], let uid = FIRAuth.auth()?.currentUser?.uid {
0
source

Source: https://habr.com/ru/post/1656175/


All Articles