Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.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
Android FireBase更新&;虽然多个用户同时执行,但仅删除一次子项_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Android FireBase更新&;虽然多个用户同时执行,但仅删除一次子项

Android FireBase更新&;虽然多个用户同时执行,但仅删除一次子项,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我的多人游戏需要删除特定的firebase子节点,并更新自己的玩家节点一次。这就是问题所在,如果同时按下按钮,可能会有多个用户同时更新自己的播放器节点 所以我的问题是,如何限制只为第一个按下按钮的用户运行DotTransaction中的逻辑?对于按下按钮的其他用户,将以不同的方式运行其他逻辑 代码如下: start = (Button) v.findViewById(R.id.start); start.setOnClickListener(new View.OnClickListener()

我的多人游戏需要删除特定的firebase子节点,并更新自己的玩家节点一次。这就是问题所在,如果同时按下按钮,可能会有多个用户同时更新自己的播放器节点

所以我的问题是,如何限制只为第一个按下按钮的用户运行DotTransaction中的逻辑?对于按下按钮的其他用户,将以不同的方式运行其他逻辑

代码如下:

start = (Button) v.findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            updatePlayer();
        }

private void updatePlayer() {  
final Iterator<DataSnapshot> playerIterator = dataSnapshot.getChildren().iterator(); 

mDatabase.child("Challenger").child("Pro").child(challengerKey).runTransaction(new Transaction.Handler() {
                                @Override
                                public Transaction.Result doTransaction(MutableData currentData) {


                                    if (currentData.getValue().toString() != null) {

                                        foundChallenger = currentData.getValue().toString();

                                        // Remove from server
                                        mDatabase.child("Challenger").child(state.getPlayer().getRank()).child(challengerKey).removeValue(new DatabaseReference.CompletionListener() {
                                            @Override
                                            public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                                                Log.d(GoogleServiceApi.TAG, "removeValueOnComplete");
                                                Log.d(GoogleServiceApi.TAG, "databaseError" + databaseError);                                                   
                                            }
                                        });

                                        // Add new data to player node
                                        ArrayList<String> newChallengerList;


                                        newChallengerList = new ArrayList<String>();
                                     newChallengerList.add(foundChallenger);

                                        Map<String, Object> challengerUpdates = new HashMap<>();
                                        challengerUpdates.put("challengerList", newChallengerList);

                                        mDatabase.child("Player").child(state.getUid()).updateChildren(challengerUpdates);


                                    } else {
                                        Log.d(GoogleServiceApi.TAG, "no current data!");
                                        return Transaction.abort();
                                    }

                                    return Transaction.success(currentData);
                                }

                                @Override
                                public void onComplete(DatabaseError databaseError, boolean committed, DataSnapshot dataSnapshot) {
                                    Log.d(GoogleServiceApi.TAG, "committed: " + committed);
                                    Log.d(GoogleServiceApi.TAG, "databaseError: " + databaseError);
                                    Log.d(GoogleServiceApi.TAG, "databaseError: " + dataSnapshot);

                                    if (!committed) {

                                    }

                                }
                            }, false);

                        } catch (NoSuchElementException e) {
                            Log.d(GoogleServiceApi.TAG, "no challenger currently!");
                        }
 }
start=(按钮)v.findviewbyd(R.id.start);
start.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
updatePlayer();
}
私有void updatePlayer(){
final Iterator playerIterator=dataSnapshot.getChildren().Iterator();
mDatabase.child(“Challenger”).child(“Pro”).child(challengerKey).runTransaction(new Transaction.Handler()){
@凌驾
公共事务.Result doTransaction(可变数据currentData){
if(currentData.getValue().toString()!=null){
foundChallenger=currentData.getValue().toString();
//从服务器中删除
mDatabase.child(“Challenger”).child(state.getPlayer().getRank()).child(challengerKey).removeValue(新数据库引用.CompletionListener()){
@凌驾
public void onComplete(DatabaseError DatabaseError,DatabaseReference DatabaseReference){
Log.d(GoogleServiceApi.TAG,“removeValueOnComplete”);
Log.d(GoogleServiceApi.TAG,“databaseError”+databaseError);
}
});
//向播放器节点添加新数据
ArrayList newChallengerList;
newChallengerList=newarraylist();
newChallengerList.add(foundChallenger);
Map challengerUpdates=new HashMap();
challengerUpdates.put(“challengerList”,newChallengerList);
mDatabase.child(“Player”).child(state.getUid()).updateChildren(challengerUpdate);
}否则{
Log.d(GoogleServiceApi.TAG,“没有当前数据!”);
返回事务。中止();
}
返回事务。成功(currentData);
}
@凌驾
public void onComplete(DatabaseError DatabaseError、boolean committed、DataSnapshot DataSnapshot){
Log.d(GoogleServiceApi.TAG,“已提交:”+committed);
Log.d(GoogleServiceApi.TAG,“databaseError:+databaseError”);
Log.d(GoogleServiceApi.TAG,“databaseError:+dataSnapshot);
如果(!已提交){
}
}
},假);
}捕获(无接触元素例外e){
Log.d(GoogleServiceApi.TAG,“目前没有挑战者!”);
}
}