![](/img/trans.png)
[英]How to update data in Firebase realtime database using Firebase 9 (modular sdk)
[英]How could I update and GET a ServerValue.increment operation via setValue in Firebase - Realtime?
我可以看到 .runTransaction 選項有一個返回快照的完成監聽器,所以我假設這個快照是客戶端剛剛執行的更新快照(“DataSnapshot currentData”)。
@NonNull
public Result doTransaction(@NonNull MutableData currentData);
/**
* This method will be called once with the results of the transaction.
*
* @param error null if no errors occurred, otherwise it contains a description of the error
* @param committed True if the transaction successfully completed, false if it was aborted or
* an error occurred
* @param currentData The current data at the location or null if an error occurred
*/
public void onComplete(
@Nullable DatabaseError error, boolean committed, @Nullable DataSnapshot currentData);
我的猜測是使用 setValue(ServerValue.increment) 比 runTransaction 快一個數量級。
問題是,似乎無法檢索結果,因為似乎沒有任何偵聽器可以可靠地返回客戶端更新的值。
/**
* ...
* @param value The value to set at this location or null to delete the existing data
* @param listener A listener that will be triggered with the results of the operation
*/
public void setValue(@Nullable Object value, @Nullable CompletionListener listener) {
setValueInternal(value, PriorityUtilities.parsePriority(this.path, null), listener);
}
這里的文檔使 promise 聽眾會給我“ THE (MY??) 操作的結果”,但是當去聽眾時它並沒有說:
/**
* This interface is used as a method of being notified when an operation has been acknowledged by
* the Database servers and can be considered complete
*
* @since 1.1
*/
public interface CompletionListener {
/**
* This method will be triggered when the operation has either succeeded or failed. If it has
* failed, an error will be given. If it has succeeded, the error will be null
*
* @param error A description of any errors that occurred or null on success
* @param ref A reference to the specified Firebase Database location
*/
public void onComplete(
@Nullable final DatabaseError error, @NonNull final DatabaseReference ref);
}
請注意,DatabaseReference 不是值更新時分支的“快照”,而只是一個實時引用......
因為返回了一個簡單的引用,所以我假設我們被迫將一個普通的 singleValueEventListener 連接到這個引用....這意味着這個讀取不會是原子的...
所以有幾種方法可能有效但我不確定:
Object db_config_ver = ServerValue.increment(1);
db_ver_ref.setValue(db_config_ver,
new DatabaseReference.CompletionListener() {
@Override
public void onComplete(@Nullable DatabaseError error, @NonNull DatabaseReference ref) {
if (error == null) {
//Should I connect a listener?? But this is NOT atomic and will bring ERRORS under heavy contention!!
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
}
);
//Maybe a simple proactive read will suffice?
long value = ref.get().getResult().getValue(Long.class);
//What If I read the ServerValue.increment() object here:
System.out.println(db_config_ver)
}
}
}
);
或者可能沒有可靠的方法來自動執行此操作。
所以也許 setValue 和 updateChildren 可以與 ServerValue.increment 運算符一起正常工作......但是正確執行 updateAnd Get的方法只能通過 runTransaction() 來實現?
當您對服務器上已存在的內容不感興趣時,可以使用ServerValue.increment()
來增加字段的值。 如果你需要根據已經存在的值進行一些操作,那么確實需要進行一個事務,先讀取數據,然后再進行更新。
第一個解決方案是事務的簡化版本,不需要與 Firebase 服務器進行往返通信。
當你想讀取一個字段被多個用戶遞增的值時,你應該附加一個持久化監聽器,這樣你就可以得到實時通知。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.