即使参考检索数据,Firebase查询在Android上也不起作用 [英] Firebase queries not working on Android even though the reference retrieves data

查看:60
本文介绍了即使参考检索数据,Firebase查询在Android上也不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试过滤我的Firebase数据,我的数据集很复杂,但这是一个示例.我能够很好地检索数据,但无法使用Firebase查询进一步过滤该数据.这是一个示例数据集:

这是我的查询:

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) {

        }
    });

引用mRef1可以完美地检索数据,只有当我对其应用查询时,它才检索空值,数据肯定与查询匹配,我甚至对值进行了硬编码,但似乎也不起作用. /p>

我错过了一些我应该在Firebase控制台上进行设置的东西吗?我确实设置了一个索引,并且解决了日志中的一个警告,该警告提示我在字段上设置一个索引,这意味着Firebase也在识别该字段和索引.

解决方案

Firebase查询返回列表中子节点的子集.

因此,如果您有位置列表:

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

您可以通过以下方式查询位置:

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

您的数据缺少一个级别:您正在查询test,但是缺少我所拥有的location1location2location3级别.如果您添加了这样一个级别,您的查询将能够匹配该子级.

您的下一个问题很可能是Firebase数据库查询只能对单个属性进行排序/过滤.这意味着对于地理位置查询,您当前只能过滤经度​​纬度,这仅使一半的地理位置查询成为可能.我建议您查看 GeoFire ,这是我们的地理查询库.它将每个项目的纬度和经度合并为一个 geohash 值,然后可以查询该值.

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:

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.

解决方案

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.

这篇关于即使参考检索数据,Firebase查询在Android上也不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆