8

I'm reading firebase.google's example seen here https://firebase.google.com/docs/database/ios/save-data#save_data_as_transactions and I am trying to write my own version. I am having trouble as the firebase example leaves me more confused than i was before i started

All i am trying to do is when a person taps a button is that it ++ (or +=1) to the count and another button removes it

Can somebody help me figure out how to do this...another user on a previous question posted links to other language answers and i tried to write it in the swift language but I failed miserably

pableiros
  • 14,932
  • 12
  • 99
  • 105
RubberDucky4444
  • 2,330
  • 5
  • 38
  • 70
  • 1
    Please don't delete one question to then post it again. If you have information to add to [your previous question](http://stackoverflow.com/questions/37668501/ios-firebase-how-to-setvalue-of-1?noredirect=1#comment62815948_37668501), click the "edit" link under it and add the information. – Frank van Puffelen Jun 07 '16 at 03:35
  • 1
    To find samples of others using Firebase transactions in swift, see [this question](http://stackoverflow.com/questions/37061536), or [this one](http://stackoverflow.com/questions/34778914/data-consistency-on-very-close-events-in-firebase/34779307#34779307) or some others from [this search](http://stackoverflow.com/search?q=%5Bfirebase%5D+transaction+swift). – Frank van Puffelen Jun 07 '16 at 03:38
  • 2
    I agree man I just found this because that transaction block in the docs is crazy confusing. I have no clue why it's so confusing like that. Incrementing is a pretty common thing, they should just have a specific function that just does it, like incrementValue() or something. – Wayne Filkins Nov 16 '16 at 18:04

2 Answers2

12

Thanks to the comments seen above I was able to get this to work

....runTransactionBlock { (currentData: FIRMutableData) -> FIRTransactionResult in

        var value = currentData.value as? Int

        if value == nil {
            value = 0
        }

        currentData.value = value! + 1
        return FIRTransactionResult.successWithValue(currentData)




    }
RubberDucky4444
  • 2,330
  • 5
  • 38
  • 70
  • 1
    Are you 100% sure it is effective? Did you test with multiple users at the same time? This looks much simpler than the docs so if you are sure it works then I would rather use this lol – Wayne Filkins Nov 16 '16 at 18:05
  • @WayneFilkins The original doc just seems to save the user did with the star. This means that if they click again, it will 'unfavourite'. – George Aug 30 '18 at 22:08
0

Seems to work in 2021 Swift 5

gv.dbRef.child("referenceToLikes ie posts/user/likes").runTransactionBlock({ (currentData: MutableData) -> TransactionResult in
        var value = currentData.value as? Int
                if value == nil {
                    value = 0
                }
                currentData.value = value! + 1
        return TransactionResult.success(withValue: currentData)
    }) { error, committed, snapshot in
        if let error = error {
          print(error.localizedDescription)
        }
    }
tiktoker
  • 11
  • 3