I am trying to increment unseenMessage by 1 everywhere everywhere the groupName is the same. The code I currently have returns a null snapshot back.
Database Structure
- Notifications
- user1
- group1
- groupName: group1
- unseenMessage: 0
- group2
- groupName: group2
- unseenMessage: 0
- user2
- group1
- groupName: group1
- unseenMessage: 0
Code:
ref.child("Notifications").queryOrdered(byChild: "groupName").queryEqual(toValue: "group1").observeSingleEvent(of: .value, with: { snapshot in
print("Snapshot:", snapshot.childrenCount)
for child in snapshot.children {
let snap = child as! DataSnapshot
print("Key:", snap)
let unreadCountRef = snap.ref.child("unseenMessage")
print(unreadCountRef)
unreadCountRef.runTransactionBlock( { (currentData: MutableData) -> TransactionResult in
var currentCount = currentData.value as? Int ?? 0
currentCount += 1
currentData.value = currentCount
return TransactionResult.success(withValue: currentData)
})
}
})
Rules:
{
"rules": {
".read": true,
".write": true,
"Notifications": {
".indexOn": ["groupName"]
}
}
}