Reputation: 168
I'm developing chat system, calculating total unread count.
here's my code
chatroomRef.child(chatInfo.getPartner_firebase_uid())
.child(mChatId)
.child("totalUnreadCount")
.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer totalUnreadCount = mutableData.getValue(Integer.class);
if(totalUnreadCount == null){
mutableData.setValue(0);
}else{
mutableData.setValue(totalUnreadCount + 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
Log.d("result", "postTransaction:onComplete:" + databaseError);
}
});
When used this code, I got permission denied. But when I used code below, it worked.
chatroomRef.child(chatInfo.getPartner_firebase_uid())
.child(mChatId)
.child("totalUnreadCount")
.setValue(1);
two codes set data change exactly same course in firebase-db. Though one code gets permission error, the other doesn't. So I think it is a problem with firebase rules. And, I gave 'true' to '.write', but got permission error.
Upvotes: 0
Views: 195
Reputation: 599766
It sounds like you don't have permission to read the value of totalUnreadCount
. A transaction writes a new value based on the existing value, so it needs both read and write permission.
Upvotes: 2