0

I have a Firebase Realtime Database with a set of Items who related to locations on a Googlemap. So every child in the Firebase have a data structure like this.

-item
latitude:"5511231165654"
longitude:"516891812013216"
postalcode: "10013"
cityname: "New York"

In my app every item from the database, where added to the related location on the Googlemap in form of a marker. Now i think it is not a good idea to send all Items to all users. It´s more efficiently to send the user´s only these items that are relevant for them. These items are that one who are in the same area with the user, in this case same area means same postal code.

To handle this i build this Query

 Query sortedByLocation = myRef.orderByChild("postalcode").equalTo(currentpostalcode);
 sortedByLocation.addListenerForSingleValueEvent(
 new ValueEventListener() {
      .
      . do something
      .
}

i got the currentpostalcode from a Geocoder also the cityname. Now the problem is sometimes the Geocoder can´t decode the postal code and return null for it, but it allways return a cityname. So i changed the query to

Query sortedByLocation = myRef.orderByChild("cityname").equalTo(currentcity);

So how can i combine these two querys ? I want to do something like this:

is postalcode == null than check for cityname

The other Question will be, it is possible to retrieve data manually at need ?

Imagine the user start the app, the app now get all items who are in the same area with the user, but now the user left this area and go into a other area.

How can i trigger to retrieve the new data for this new area without to restart the app ?

worl44
  • 165
  • 2
  • 2
  • 8

1 Answers1

0

Firebase Queries can only contain a single sort/filter condition.

Sometimes you can combine multiple values into a single property to get the behavior you want. For an example of that, see my answer here: Query based on multiple where clauses in firebase

But in general it sounds like you might be better off using GeoFire, which uses GeoHashes (a way of combining longitude and latitude into a single property) to allow filtering items based on their distance to a point. Note that at this moment, the updated version of GeoFire for Java is still in the works.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807