2

I'm trying to use Firebase and runTransactionBlock to update an Array on a server. I previously was able to add the uid of userToFollow to user's Following node using the solution found on Synchronized Array (for likes/followers) Best Practice [Firebase Swift]. Now I am trying to enable unfollowing by removing the uid of userToFollow from user's Following node, but despite the runTransactionBlock succeeding the data is not getting updated. Here is my code:

static func unfollow(user: FIRUser, userToUnfollow: FIRUser) {
    self.database.child("users/"+(user.uid)+"/Following").runTransactionBlock({ (currentData: FIRMutableData!) -> FIRTransactionResult in
        var value = currentData?.value as? Array<String>
        if (value == nil) {
            print("unfollow - user has never followed user ID " + userToUnfollow.uid)
            FIRTransactionResult.abort()
        } else {
            value = value!.filter() {$0 != userToUnfollow.uid}
        }
        currentData.value = value!
        return FIRTransactionResult.successWithValue(currentData)
    }) { (error, committed, snapshot) in
        if let error = error {
            print("unfollow - update user unfollowing transaction, please try again - EXCEPTION: " + error.localizedDescription)
        } else {
            print("unfollow - update user unfollowing success")
        }
    }
}

I am trying to update the array by first sorting out the uid of userToUnfollow then syncing this data with Firebase. How can I get this to actually remove the node? Thanks.

Community
  • 1
  • 1
Tim Lupo
  • 259
  • 2
  • 9

0 Answers0