2

I have been trying to implement a "follow" function on my app. Essentially when a user hits the "follow" button, we run a runTransactionBlock to update integer values we are storing on Firebase database for the user and the account they are following. The issue is that I am able to update the counter for the user (say John in example below) , but I am not able to update the counter for the user I am following (say olivia in example below).

Currently the Firebase nodes look as such:

user_profiles{
      UID1:{
           name: john
           following: 1 //code will update for my account
           followers: 0
      },
      UID2:{
           name: olivia
           following: 0
           followers: 0 //code will not update count for person i am trying to follow

I have referenced the following, however I am still facing issues with getting this to work. If anyone can please glance through and point me in the right direction, it would be greatly appreciated.

https://www.firebase.com/docs/ios/guide/saving-data.html

Firebase database help - Swift

Upvote/Downvote system within Swift via Firebase

var guestUIDToPass = String()
var loggedInUser = AnyObject()

@IBAction func didTapFollow(sender: AnyObject) {
 following() 
}



func following() {

        self.loggedInUser = FIRAuth.auth()?.currentUser


//updating count for user, works perfectly        

self.databaseRef.child("user_profiles").child(self.loggedInUser.uid).child("following").runTransactionBlock({
            (currentData:FIRMutableData!) in
            var value = currentData.value as? Int
            if (value == nil) {
                value = 0
            }
            currentData.value = value! + 1
            return FIRTransactionResult.successWithValue(currentData)
        })

//updating count for person user is following, doesn't update firebase

        self.databaseRef.child("user_profiles").child("\(self.guestUIDToPass)").child("followers").runTransactionBlock({
            (currentData:FIRMutableData!) in
            var value = currentData.value as? Int
            if (value == nil) {
                value = 0
            }
            currentData.value = value! + 1
            return FIRTransactionResult.successWithValue(currentData)

        })
    }
Community
  • 1
  • 1
gk103
  • 377
  • 5
  • 15
  • 1
    Maybe because `followers` is actually `follower` in your JSON tree..Are you using Swift3? – Dravidian Sep 20 '16 at 19:00
  • @Dravidian double checked they are both the same. I typed up the JSON tree on my question, just updated it. Currently using swift 2 – gk103 Sep 20 '16 at 19:22
  • Make sure you are getting your `guestUIDToPass` right, and why are you using same child `guestUIDToPass` in both transaction? – Dravidian Sep 20 '16 at 19:47
  • Doh! Typed that in wrong on my question just updated it :(. But on Xcode it shows correct. Yes guestUIDToPass is showing the correct string. I even tried hard coding the guest UID into the code with no luck. – gk103 Sep 20 '16 at 19:50

1 Answers1

3

Try:-

let prntRef = FIRDatabase.database().reference().child("user_profiles").child(whomIFollowedUID).child("following")

prntRef.runTransactionBlock({ (following) -> FIRTransactionResult in
    if let followNum = following.value as? Int{

        following.value = followNum + 1
        return FIRTransactionResult.successWithValue(following)
    }else{

        return FIRTransactionResult.successWithValue(following)

    }
    }, andCompletionBlock: {(error,completion,snap) in

            print(error?.localizedDescription)
            print(completion)
            print(snap)
        if !completion {

            print("The value wasn't able to Update")
            }else{
            //Updated
        }
})
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • This works, thanks! i made a small mod, which was to add another child: `let prntRef = FIRDatabase.database().reference().child("user_profiles").child(whomIFollowedUID).child("following")` – gk103 Sep 20 '16 at 21:28