How to increase a value in Firebase Realtime Database?

ε祈祈猫儿з 提交于 2019-12-24 20:13:03

问题


I have this simple struct:

{
    "items": {
        "item1": {
            "likes": 4
        },
        "item2": {
            "likes": 5
        }
    }
}

I enable offline mode:

Firebase.getDefaultConfig().setPersistenceEnabled(true);

Multiple users can increase the "likes" fields at the same time. How can I increase the likes filed by 1 in offline mode? If I get the likes value than set it with the increased value, what happens it the device reaches the server but another users before that have increased the same likes field? Will it overwite the value?

I can imagine my data structure is not so good because I'm new to NoSQL. I can add Firebase userids to the likes fields instead of a counter integer and summarize them to get the result. But this means lots of unneccessary data beacuse I don't need to know who liked I just need the number of likes.

Please help me to find out the best solution! Thank you!

来源:https://stackoverflow.com/questions/45276327/how-to-increase-a-value-in-firebase-realtime-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!