firebase / geofire-js

GeoFire for JavaScript - Realtime location queries with Firebase
MIT License
1.45k stars 345 forks source link

What is the best way to response all query result? #134

Closed agumack closed 7 years ago

agumack commented 7 years ago

Hi guys,

Same title, how i can response all result query? Ex: var geoQuery = geoFire.query({ center: [10.38, 2.41], radius: 10.5 }).then((allResult)=>{ //some code })

I ask because i use geofire with react-native and have performance problem. my code:

var onKeyEnteredRegistration = geoQuery.on("key_entered", function (key, location, distance) {
            console.log(key + " entered query at " + location + " (" + distance + " km from center)");
            firebaseApp.getFanpageData(key).then((data) => {
                this.setState({this.state.datasource.concat([data.val()])})
                //view render when state changed => not good.
                //if i can get all resource as "keyIds" and use Promise.all(queries) is awesome!!!
            }).catch((error) => {
                console.log("error", error);
            })
        });

thanks so much!!!

jwngr commented 7 years ago

You can store everything in a list and then use the on("ready") event to update state:

var keys = [];

var onKeyEnteredRegistration = geoQuery.on("key_entered", function (key, location, distance) {
  console.log(key + " entered query at " + location + " (" + distance + " km from center)");
  keys.push(key);
});

var onReadyRegistration = geoQuery.on("ready", function (key, location, distance) {
  // Update state using the keys array
rezasazesh commented 4 years ago

I cannot access this inside of the geoQuery.on("ready" function so when I use this.setState in react native I get "cannot read property 'setState' of undefined ...

rezasazesh commented 4 years ago

Okay if you run into the same problem I did in React-native, you will need to use an arrow function for the callback of the "ready" function like below. Also no point assigning variables to your functions

    let requests_in_desired_redius = [];

    geoQuery.on('key_entered', function(key, location, distance) {
      requests_in_desired_redius.push({key, location, distance});
    });

    geoQuery.on('ready', () => {
      this.setState({requests_in_desired_redius});
    });