Filter Results Using Geofire + Firebase

I am trying to figure out how to request a filter using Geofire. Suppose I have restaurants with a different category. and I want to add this category to my query. How can I do it?

One of the ways that I am now accessing the key with Geofire is to start the for loop through each key and get the restaurant and insert the corresponding restaurant into the array. They seem so ineffective. Is there any other way to do this?

Ideally, I get the filtered results and load each item when they are shown.

Hooray!

+3
source share
2 answers

Firebase requests can only be filtered by one condition. Geofire is already doing a few "magic" so that it can filter both in longitude and in latitude. Adding another property to this equation may be possible, but far superior to what Geofire handles by default. See GeoFire: How to add additional conditions to the request?

If you only want to access one category at a time, you can place the node level restaurants for each category and specify Geofire in one category.

/category1 item1 g: "pns0h0mf2u" l: [-53.435719, 140.808716] item2 g: "u417k3dwub" l: [56.83069, 1.94822] /category2 item3 g: "8m3rz3s480" l: [30.902225, -166.66809] /items item1: ... item2: ... item3: ... 

In the above example, we have two categories: category1 with two elements and category2 for just 1 element. For each element, we see the data that Geofire uses: geohash, longitude and latitude. We also keep one list with other properties of these 3 elements.

But more often than not, you simply perform additional filtering in the client code. If you are worried about the effectiveness of this: measure it, share the code, JSON data and measurements.

+8
source

This is an old question, but I saw it in several places on the Internet, so I thought I could share one trick that I used.

Problem

If you have a large collection in your database, possibly containing hundreds of thousands of keys, for example, it may not be possible to capture all of them. If you are trying to filter results based on location in addition to other criteria, you are stuck with something like:

  • Run a location request
  • Scroll through each returned geophile key and take the corresponding data in the database
  • Check each returned piece of data to make sure it meets other criteria.

Unfortunately, this is a lot of network requests, which is rather slow.

More specifically, let's say we want all users to be within, for example. 100 miles at a specific location that are men and ages 20 to 25 years. If 10,000 users are within 100 miles, this means that 10,000 network requests capture user data and compare their gender and age.

Workaround:

You can store the data needed for your comparisons in the geofin key itself, separated by a delimiter. Then you can simply split the keys returned by the geofire request to access the data. You still have to filter them, but this is much faster than sending hundreds or thousands of requests.

For example, you can use the format:

UserID*gender*age , which might look something like facebook:1234567*male*24 . The important points are

  • Separate data points using a delimiter
  • Use a valid character for the delimiter - "It can include any unicode character except. $ # [] / And ASCII control characters 0-31 and 127.)"
  • Use a character that won't be found elsewhere in your database - I used *, but this may not work for you. Do not use characters from -0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz , as they are fair games for keys generated by firebase push()
  • Choose a consistent data order — in this case, UserID first, then gender, then age.

You can store up to 768 bytes of data in firebase keys, which is very important.

Hope this helps!

+3
source

Source: https://habr.com/ru/post/1242651/


All Articles