Firebase - 多位用户使用旧值同时更新同一对象 [英] Firebase - Multiple users simultaneously updating same object using its old value

查看:140
本文介绍了Firebase - 多位用户使用旧值同时更新同一对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Firebase和数据库,所以请原谅,如果这个问题有一个简单的解决方案,我忽略了。

我跟踪用户通过Android应用更新Firebase数据库的次数。它现在的工作方式是,在交互时,用户的应用程序查找数据库上的当前计数(使用在新的ValueEventListener中定义的addListenerForSingleValueEvent()和onDataChange()方法),并添加一个,然后将计数设置为新值使用mRef.setValue()其中mRef是数据库的引用。

我担心的问题是如果大量用户交互会发生什么与数据库同时在一起; Firebase是否负责确保值的正确读取和增加,或者是否存在大量的重叠和潜在的数据丢失。



感谢您花时间帮忙!当处理可能被同步修改损坏的复杂数据(如增量计数器)时,Firebase提供了一个事务操作。

你给这个操作两个参数:一个更新函数和一个可选的完成回调函数。更新函数将数据的当前状态作为参数,并返回想要写入的新状态。例如,如果我们想要增加特定博客上的upvotes数量,我们会写下如下的事务(传统代码):

  Firebase upvotesRef = new Firebase(https://docs-examples.firebaseio.com/android/saving-data/fireblog/posts/- JRHTHaIs-jNPLXOQivY / upvotes); 
upvotesRef.runTransaction(new Transaction.Handler(){
@Override
public Transaction.Result doTransaction(MutableData currentData){$ b $ if(currentData.getValue()== null) {
currentData.setValue(1);
} else {
currentData.setValue((Long)currentData.getValue()+ 1);
}
return .success(currentData); //我们也可以通过调用Transaction.abort()
}
@Override
来中止。public void onComplete(firebaseError firebaseError,boolean committed,DataSnapshot currentData){
//这个方法将被调用一次,结果为

});

传统来源
$ b

新的firebase版本源代码


I'm just getting started with Firebase and databases in general so please excuse me if this problem has a simple solution I'm overlooking.

I'm keeping track of a count that users update on the Firebase database through an Android app. The way it works right now is that upon interaction the user's app looks up the current count on the database (using a addListenerForSingleValueEvent() and onDataChange() method defined within the new ValueEventListener) and adds one to it and then sets the count to this new value using mRef.setValue() where mRef is the reference to the database.

The issue I'm worried about is what would happen if a large number of users interacted with the database together at the same time; does Firebase take care of making sure that the value is read and incremented properly or is there a lot of overlap and potentially a loss of data because of that.

Thanks for taking the time to help out!

解决方案

When working with complex data that could be corrupted by concurrent modifications, such as incremental counters, Firebase provides a transaction operation.

You give this operation two arguments: an update function and an optional completion callback. The update function takes the current state of the data as an argument and will return the new desired state you would like to write.

For example, if we wanted to increment the number of upvotes on a specific blog post, we would write a transaction like the following (Legacy code):

    Firebase upvotesRef = new Firebase("https://docs-examples.firebaseio.com/android/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY/upvotes");
upvotesRef.runTransaction(new Transaction.Handler() {
    @Override
    public Transaction.Result doTransaction(MutableData currentData) {
        if(currentData.getValue() == null) {
            currentData.setValue(1);
        } else {
            currentData.setValue((Long) currentData.getValue() + 1);
        }
        return Transaction.success(currentData); //we can also abort by calling Transaction.abort()
    }
    @Override
    public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) {
        //This method will be called once with the results of the transaction.
    }
});

Legacy source

New firebase version source

这篇关于Firebase - 多位用户使用旧值同时更新同一对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆