How can I check if a value exists already in a Firebase data class Android

前端 未结 5 1388
孤独总比滥情好
孤独总比滥情好 2020-12-01 09:49

I want to check if the bus number already exists in the database of Firebase.

Here\'s my sample code. I\'ve been searching for the past days but I can\'t find the rig

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 10:31

    It is difficult to guess the problem because you do not show how busNum and busNumber are defined and managed. Is busNumber a String?

    push() creates a reference to an auto-generated child location. The auto-generated key looks something like -KPB_cS74yoDaKkM9CNB.

    The statement postRef.push().setValue(busNumber) stores value busNumber in location BusNumber/.

    The statement dataSnapshot.child(busNum).exists() tests for the existence of a value at location BusNumber/. It will not be true unless busNum is one of the keys created by push().

    It's not clear how you want your data structured. If your bus numbers are Strings and are unique, you do not need to generate a key with push(). You could store the existence of bus numbers using:

    postRef.child(busNumber).setValue(true)

提交回复
热议问题