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