Toggle Boolean in Firebase Real-Time Database

I am accessing my firebase database from my android application. I need to implement like functionality like facebook. Therefore, when the user clicks the button, if the message has already been liked, it becomes disordered and vice versa. For this purpose, I first check if the liked logical field is true in firebase, and if so, update it to false and vice versa. My current approach involves reading the liked field and then writing it (making 2 DB calls).

Is there any built-in Firebase solution for this?

Switching a boolean value to a Firebase database

+5
source share
1 answer

This method is a little nasty and not really good, but it works faster (I think).

Instead of boolean enter your integer parameter. Each time a similar button is clicked, add a +1 value to it, and whenever you read this data, you just need to confirm that it is odd or even. Odd for like, even for not similar, or vice versa.

0
source

Source: https://habr.com/ru/post/1262821/


All Articles