Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java/Android中的Firebase:事务被后续集合覆盖_Java_Android_Exception_Firebase - Fatal编程技术网

Java/Android中的Firebase:事务被后续集合覆盖

Java/Android中的Firebase:事务被后续集合覆盖,java,android,exception,firebase,Java,Android,Exception,Firebase,我使用Firebase SDK for Java/Android的多人游戏有游戏室,4名玩家可以一起玩 为了实现配对,我为玩家可以加入的每个房间都提供了Firebase参考资料。这些引用中的每一个都有4个插槽(作为子节点),其中包含参与的玩家的UUID,如果插槽仍然可用,则包含一个空字符串 为了防止两个(或更多)玩家同时占用同一个插槽,我使用了事务。以下代码是否正确 private int mUserSlot; firebaseReference.runTransaction(new Tran

我使用Firebase SDK for Java/Android的多人游戏有游戏室,4名玩家可以一起玩

为了实现配对,我为玩家可以加入的每个房间都提供了Firebase参考资料。这些引用中的每一个都有4个插槽(作为子节点),其中包含参与的玩家的UUID,如果插槽仍然可用,则包含一个空字符串

为了防止两个(或更多)玩家同时占用同一个插槽,我使用了事务。以下代码是否正确

private int mUserSlot;

firebaseReference.runTransaction(new Transaction.Handler() {

    @Override
    public Result doTransaction(MutableData currentData) {
        for (int s = 0; s <= 3; s++) { // loop through all slots
            final String slotValue = currentData.child(slotFromLocalID(s)).getValue(String.class);
            if (slotValue == null || slotValue.equals("") || slotValue.equals(mUserUUID)) { // if slot is still available
                currentData.child(slotFromLocalID(s)).setValue(mUserUUID);
                mUserSlot = s;
                return Transaction.success(currentData);
            }
        }
        return Transaction.abort();
    }

    @Override
    public void onComplete(FirebaseError error, boolean committed, DataSnapshot currentData) {
        if (error == null) {
            if (committed) {
                System.out.println("User is now in slot "+mUserSlot);
            }
            else {
                System.out.println("User could not join, all slots occupied");
            }
        }
        else {
            System.out.println("Error: "+error.getMessage());
        }
    }

});
这到底是什么意思?我认为这些事务是为了防止并发访问相互覆盖的相同字段。这是否意味着应用程序的其他部分在没有事务的情况下写入该字段


在这种情况下,我可以像处理
一样处理它吗!已提交
?这意味着,在这两种情况下,我想要写入的值在事务完成后都不存在,对吗?

是的,该消息表示您的一个客户端正在使用
set
而不是
transaction
将事务中止到同一位置写入。我强烈建议不要在使用事务的位置使用
set

The transaction was overridden by a subsequent set