Here i'm working in a chat application, i want to maintain the unread count for the recipient side. My json structre in console looks like
user_history | |--> user_a_uuid |-> last_msg |-> last_msg_timestamp |-> unread_count
the scope of this question is, how to increment the value of unread_count
. Prior incrementing i have the user_a_uuid
and the node user_history
& unread_count
as a constant. So i tried as follows
dbRef.child("user_history")
.child(user_a_uuid)
.child("unread_count")
.runTransactionBlock { (unreadCount) -> TransactionResult in
var value = unreadCount.value as? Int
if value == nil {
value = 0
}
unreadCount.value = value! + 1
return TransactionResult.success(withValue: unreadCount)
}
Actually to be precise i borrowed this solution from here. I also tried to follow the doc. But the thing is that, i don't want to run transaction on the root node as in the doc. I want to run the transaction only on the unread_count
doc.
Problem I'm facing:
After attempting this code, the problem i face is that, unread_count
node getting deleted as soon and while i return the TransactionResult
inside the completion block, it added unread_count
. The problem is that the value is not incremented due to unread_count
getting deleted. It always stays to 1
.
Thanks in advance.