Firebase + Geofire : ordering by time

1,411 views
Skip to first unread message

Clement Mellinas

unread,
Jan 20, 2015, 1:34:56 PM1/20/15
to fireba...@googlegroups.com


Hello,



In my app I use both Firebase and Geofire

Both work well but I got a case where I am stuck.

Geofire gives me data ordered by distance. I would like to get data ordered by another filter which will not be related to distance (like a datetime for example).  I tried with priorities or adding data directly to Geofire childs but it seems not possible.

I can use filters on client side but it forces me to load "all" the data client side.


Let"s suppose a specific case : geolocalised ads. How would you manage to do that query :  "20 last ads on 10 miles"

We got two firebase nodes :   "ads-locations" with geofire structure and "ads-data" with data like { "content": "my content", "time" : "datetime", ...)

Both are linked with a push id.


For now I can get : 

20 randoms ads on 10 miles using a counter and incrementing/decrementing it on geofire events

- all ads on 10 miles ordered by time using a filter on client side  

I can use my counter to limit the request only if I know data are ordered by time.



Thanks,

Clément

Jacob Wenger

unread,
Jan 20, 2015, 6:14:17 PM1/20/15
to fireba...@googlegroups.com
​Hey Clement,

GeoFire is strictly for doing geolocation queries, not timestamped queries. Thankfully, you can do timestamped queries using the regular Firebase library. If you want a combination of geolocation queries and timestamped queries though, you will need to do some client-side filtering. Firebase/GeoFire do not currently support these kinds of two-dimensional queries.

To do a timestamped query with Firebase, you should use Firebase.ServerValue.TIMESTAMP. You can then query things since a certain timestamp sometime by doing:

ref.child('ads-data').orderByChild('datetime').startAt(sometime).on('child_added'), function(childSnapshot) {
  // ...
});

Jacob​

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/b07a1bb1-14bd-4e18-a34b-14e79865d411%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Clement Mellinas

unread,
Jan 21, 2015, 10:00:32 AM1/21/15
to fireba...@googlegroups.com

Hey Jacob,


Thanks for the explanation. 

When you say currently, it means maybe in the future it will be possible to do two dimensional?


For now, I think I will load all Geolocation results on an area. And then using Limit and OrderBy filters on client side should work for now. Until there are too many results given by the Geolocation query 

Maybe a cron job archiving ads whose dates are > 1 month and moving there locations is the only way to prevent future performance issues.


Thanks, 

Clément

Jacob Wenger

unread,
Jan 21, 2015, 11:41:45 AM1/21/15
to fireba...@googlegroups.com
Hey Clement,

Hopefully at some point we will add even richer query capabilities like you want/need. There is not work on this at the moment, but I've heard it thrown around from time to time. There certainly are valid use cases for it.

Let me know if you run into any troubles.
Jacob

Tres Hombres

unread,
May 14, 2015, 5:23:39 PM5/14/15
to fireba...@googlegroups.com
Hi Jacob, I am using GeoFire both in iOS and Javascript.

I noticed a couple of differences:

1), the Javascript callback for "key_entered" includes distance and location

geoQuery.on("key_entered", function(messageId, messageLocation, distanceInKm) {
  // Specify that the message has entered this query
     ...
}

   and the iOS callback does not include distance:

    self.enterQueryHandle = [self.geoFireCircleQuery observeEventType:GFEventTypeKeyEntered withBlock:^(NSString *key, CLLocation *location) {

}

It would be nice to have the 

2) In the Javascript callback, the results are ordered by key, which means time.  In my case, that is what I want.  While developing the iOS code, I assumed similar behavior and noticed that the keys where out of order.  From Clement's comment above, it seems they are ordered by distance in the iOS callback.

It would be great to have both the Javascript and iOS libraries share the same behavior to keep respective code structure for both platforms.

Of course it would be nice to define in the sort criteria when you instantiate the geoQuery.   The last commit to geoFire was in February, any plans to make any updates?

Nick K

unread,
May 25, 2015, 10:24:54 PM5/25/15
to fireba...@googlegroups.com
I'm having the similar issue and trying to figure out a way to retrieve the distance in the callback for iOS.  Any pointers?

Jonny Dimond

unread,
May 26, 2015, 8:13:52 PM5/26/15
to fireba...@googlegroups.com
Hi,

1) on iOS you can use CLLocation to get the distance [1].
2) On JavaScript the events are only coincidentally ordered by key. GeoFire does several Firebase queries under the hood and having a consistent ordering for the events is not really possible, it depends heavily on how the queries happen to fall under the hood...

Jonny

Nick Kalkounis

unread,
May 27, 2015, 3:54:06 PM5/27/15
to fireba...@googlegroups.com
Thanks Jonny

--
You received this message because you are subscribed to a topic in the Google Groups "Firebase Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/firebase-talk/LT5zsYcAFK8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to firebase-tal...@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.

Jacob Wenger

unread,
May 27, 2015, 11:06:37 PM5/27/15
to fireba...@googlegroups.com
Also, the fact that there have been no commits since February is not an indication that the project is stagnant. There just have been no bugs reported and there have been no features we felt worthy of adding at this point. I still urge anyone and everyone to use the library! The library works really well!

Jacob

Reply all
Reply to author
Forward
0 new messages