0

So I am using this function to control the attending and not attending feature in my app for an event. My firebase database contains an attend count for each event. I am also using transaction blocks to facilitate the task of multiple clicks on that button and making sure the data count is not corrupted when that happens.

Below is the function that I use when im attempting to alter my database to make sure someone is attending an event.

static func create(for event: Event?, success: @escaping (Bool) -> Void) {
        // 1
        guard let key = event?.key else {
            return success(false)
        }
        guard let uid = Auth.auth().currentUser?.uid else{
            return
        }
        let attendData = ["Attending/\(key)/\(uid)" : true,
                          "users/\(uid)/\("Attending")/\(key)" : true]

        // 2
        let ref = Database.database().reference()
        ref.updateChildValues(attendData) { (error, _) in
            if let error = error {
                assertionFailure(error.localizedDescription)
                return success(false)
            }

            // 3
            //return success(true)

            let attendCountRef = Database.database().reference().child("events").child(key).child("attend:count")
            attendCountRef.runTransactionBlock({ (mutableData) -> TransactionResult in
                let currentCount = mutableData.value as? Int ?? 0
                print(mutableData.value)
                print(currentCount)

                mutableData.value = currentCount + 1

                return TransactionResult.success(withValue: mutableData)
            }, andCompletionBlock: { (error, _, _) in
                if let error = error {
                    assertionFailure(error.localizedDescription)
                    success(false)
                } else {
                    success(true)
                }
            })
        }
    }

This is a sample of my database structure

"events" : {
    "ABP" : {
      "attend:count" : 2,
      "event:category" : "Seize The Night",
      "event:city" : "Philadelphia",
      "event:date" : {
        "end:date" : "08/09/2017",
        "end:time" : "7:00 PM",
        "start:date" : "08/10/2017",
        "start:time" : "12:00 PM"
      }
}

The transaction block it seems. It is not grabbing the attend count. It is returning as zero and not 2. Which isn't properly altering it on the database side. Any help is appreciated

Ron Baker
  • 381
  • 6
  • 16
  • Firebase invokes your transaction handler with its best guess for the current value. Initially this may very well be null, since it may not know that value yet. That why your transaction handler may be invoked multiple times and must always be able to handle null. See Kato's explanations here: https://stackoverflow.com/q/28811037 and here https://stackoverflow.com/q/16359496 and mine here https://stackoverflow.com/a/33578953 and here https://stackoverflow.com/a/45715044 – Frank van Puffelen Jan 24 '18 at 15:22
  • @FrankvanPuffelen thanks I will look at this – Ron Baker Jan 24 '18 at 17:13
  • @FrankvanPuffelen do you see anything wrong with my current implementation though? – Ron Baker Jan 24 '18 at 17:16
  • What do these two statements print? `print(mutableData.value) print(currentCount)` – Frank van Puffelen Jan 24 '18 at 17:55
  • just error checking I guess @FrankvanPuffelen – Ron Baker Jan 24 '18 at 18:01
  • I understand. I'd like to know what they print, since that might provide a clue as to what's going wrong for you. – Frank van Puffelen Jan 24 '18 at 20:52

0 Answers0