Firebase queries not working on Android even though the reference retrieves data

我怕爱的太早我们不能终老 提交于 2020-07-11 06:51:05

问题


I am trying to filter my Firebase data, my dataset is complex but here is a sample. I am able to retrieve data just fine but I am unable to filter that data any further using the Firebase queries. Here is a sample dataset:

enter image description here

Here is my query:

private double locStart;
private double locEnd;
Firebase mRef;
Firebase mRef1;

    mRef = new Firebase("https://test.firebaseio.com/");
mRef1 = mRef.child("test");

    final Intent intent = getIntent();
    final Inputs inputs = (Inputs) intent.getSerializableExtra("inputs");

    locStart = inputs.getLocLat() - 0.008983;
    locEnd = inputs.getLocLat() + 0.008983;

    Query filterData = mRef1.orderByChild("locLat").startAt(locStart).endAt(locEnd);

    filterData.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Match matchd = dataSnapshot.getValue(Match.class);
            Offer.setText(matchd.getfbName());
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

The reference mRef1 retrieves data perfectly, its only when I apply the query to it is when it retrieves null values, the data certainly matches the query, I have even hard coded the values but that does not seem to work either.

Am I missing something I should have setup on the Firebase console? I did setup an index and that solved a warning in my log which was suggesting me to set one up on the field meaning that Firebase is recognising the field and the index as well.


回答1:


Firebase queries return a subset of the child nodes in a list.

So if you have a list of locations:

locations: {
  location1: { lat: 42.010185, lon: -33.010185 },
  location2: { lat: -11.19645, lon:  52.461219 },
  location3: { lat: 33.14518,  lon: -11.128746 }
}

You could query the locations with:

ref.child("locations").orderByChild("lat").startAt(-12.0).endAt(-10)

Your data is missing a level: you're querying test, but are missing the location1, location2, location3 level that I have under it. If you add such a level, your query will be able to match the child.

Your next problem is likely going to be that Firebase Database queries can only order by/filter on a single property. That means that for geo-queries, you can currently only filter on longitude or on latitude, which makes for only half a geo-query. I recommend that you look into GeoFire, which is our library for geo-querying. It combines latitude and longitude of each item into a single geohash value, which can then be queried.



来源:https://stackoverflow.com/questions/41569846/firebase-queries-not-working-on-android-even-though-the-reference-retrieves-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!