繁体   English   中英

我如何通过 Firebase 中的 setValue 实时更新和获取 ServerValue.increment 操作?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM