Android Firebase数据库,检查数据库是否包含对象

时间:2018-07-23 16:13:18

标签: java android firebase firebase-realtime-database

我将Firebase DB用作Android应用程序的数据库。我需要检查db是否包含具有名称的对象,该名称会赋予用户。如果是,我将引用保存到该文件并加载数据,例如-显示有关对象名称不正确的信息。

例如,我使用了: Firebase querying data

代码:

private void setListReferance(){
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle(Config.ADDLIST);

            // Set up the input
            final EditText input = new EditText(this);
            // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
            input.setInputType(InputType.TYPE_CLASS_TEXT);
            builder.setView(input);

            // Set up the buttons
            builder.setPositiveButton(Config.OK, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String listName = input.getText().toString();
                    boolean correctName = checkListName(shoplistsRef, listName);

                    if(correctName){
                        listRef = shoplistsRef.child(listName);
                        myAdapter.setItemsListRef(listRef);
                        myAdapter.notifyDataSetChanged();
                        firebaseUpdate();
                    }
                    else{
                        String messege = "Niepoprawna nazwa listy";
                        Toast.makeText(MainActivity.this,
                                messege, Toast.LENGTH_LONG).show();
                    }
                }
            });
            builder.setNegativeButton(Config.CANCEL, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            builder.show();
}

检查db是否包含的方法。对于测试,我添加了吐司。

private boolean checkListName(DatabaseReference ref, String listName){
    boolean[] result = new boolean[1];
    Query query = ref.child(listName);

    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            if (dataSnapshot.exists()) {
                Toast.makeText(MainActivity.this,
                        ref.toString(), Toast.LENGTH_LONG).show();


                    Toast.makeText(MainActivity.this,
                            dataSnapshot.getValue(String.class), Toast.LENGTH_LONG).show();

                result[0] = true;
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return result[0];
}

问题是当我写正确的名称(DB拥有它)时,从else块setListReferance()中显示的第一个吐司(根本不应该显示),然后从if(dataSnapshot.exists()){}中显示两个吐司。

为什么会发生?我认为同步和异步方法存在问题,但我不知道如何解决。

1 个答案:

答案 0 :(得分:0)

Firebase是一种异步设计。查阅Google文档Read and Write using Firebase

  

Firebase数据被写入FirebaseDatabase引用,并通过将异步侦听器附加到该引用来进行检索。数据的初始状态会触发一次侦听器,数据的任何更改都会触发一次。

这样做:“ boolean [] result = new boolean 1;”由于异步实现而无法使用。确保有时您可能会由于某种原因而幸运,并且会看到价值更新,但不一致。原因是,您要在数据包运行到Firebase数据库的同时声明该值,以获取其值,然后这就是混乱发生的地方,该值会更新,然后然后数据包到达返回,但为时已晚,值已经更新。

一种解决方法是使用这样的回调侦听器:

Integer
相关问题