Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.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_Json_Firebase Realtime Database - Fatal编程技术网

Android Firebase:检查节点是否存在

Android Firebase:检查节点是否存在,android,json,firebase-realtime-database,Android,Json,Firebase Realtime Database,我有一个检查节点是否可用的函数。功能如下 public class Utilities { static boolean availability; static FirebaseDatabase fireDb = FirebaseDatabase.getInstance(); static DatabaseReference dbRef1; private static boolean checkAvailability(String ID) {

我有一个检查节点是否可用的函数。功能如下

public class Utilities {
    static boolean availability;
    static FirebaseDatabase fireDb = FirebaseDatabase.getInstance();
    static DatabaseReference dbRef1;
    private static boolean checkAvailability(String ID) {
        /**check in firebase if the generated ID exists. if it exists return false, else return true**/
        final String id = ID;
        availability = true;

            dbRef1 = fireDb.getReference("xdsdads");
            dbRef1.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    try {
                        if (dataSnapshot.hasChild(id) || dataSnapshot.hasChild("Room:" + id)) {
                            Log.d("key", "Key exists!");
                            availability = false;
                        } else {
                            availability = true;
                        }
                    } catch (Exception e) {
                        Log.d("Error", "error getting key" + e.toString());
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        Log.d("Availability","" + availability);
        return availability;

    }


    public static String generateID() {
        /**generates ID **/
        String ID = "";
        Random random = new Random();
        for (int i = 0; i < 8; i++) {
            if (i < 4) {
                ID = ID + giveAlphabet(random.nextInt(26));
            } else {
                ID = ID + random.nextInt(9);
            }
        }
        Log.d("ID",ID);

        if (checkAvailability(ID)) {
            return ID;
        } else {
            return generateID();
        }
    }

    public static String giveAlphabet(int i) {
        /**returns an alphabet from A-Z**/
        String[] alphabet = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        String output = "";
        try {
            if (i >= 0 && i < 26) {
                output = alphabet[i];
            } else {
                throw new Exception("Value not between 0-26 cannot generate Alphabet");
            }
        } catch (Exception e) {
            Log.d("Exception at Beacon", e.toString());
        }
        return output;
    }

sampledb是我的根。我正在检查参数ID是否存在于db中。但从未调用onDataChange方法。因此,我无法检查给定节点是否存在。

您将同步模式与异步模式混淆了。您的函数将调用Firebase获取数据,但在得到答案之前立即返回。Firebase必须呼叫网络以获取数据,这需要时间。当答案到达时,为时已晚,因为您已经尝试返回一个值


您需要做的是让函数将回调作为附加参数。当数据到达时,使用值调用此回调。要做到这一点,您几乎肯定首先还需要重写如何拨打电话。如果您将上游逻辑设计为在此处预期同步操作,则还需要更改该上游逻辑以容忍异步行为。

该方法将始终返回true。您应该创建一个回调接口,并将其作为参数传递给您的方法。变量“availability”是全局变量,generateID不会考虑您的ID是否存在,因为它会立即返回true。
 {
  "PKWC1865" : "77.6709266,12.8541147",
  "QKAR6584" : "79.6944701,11.1072002",
  "Room:ANWE3226" : {
    "FREESLOTS" : "2,3,4",
    "USER1" : "BC:QKAR6584",
    "USER2" : "NONE",
    "USER3" : "NONE",
    "USER4" : "NONE"
  }
}