如何在Android中的Firebase事务期间在MutableData中添加新的键值对?

时间:2016-03-02 13:55:13

标签: android firebase firebase-realtime-database

我需要通过Android中的事务添加新的键值对。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

MutableData中的doTransaction()对象有一个child()方法,可用于读取/写入子数据。

ref.runTransaction(new Transaction.Handler() {
  @Override
  public Transaction.Result doTransaction(MutableData currentData) {
    currentData.child("my new child").setValue(1);
    return Transaction.success(currentData);
  }
  @Override
  public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) {
    //This method will be called once with the results of the transaction.
  }
});