Search code examples
iosswiftfirebasegeofire

Improve GeoFire query performance - Firebase


I'm building an app in Swift where Users( can post the locations of event they witness in the street. Each event has a lifetime of 45 mins, then it does no appear on the map after that threshold.

I built my Json tree like this (locations and events share the same id - not represented here) :

firebaseapp {
  locations {
    g
    l {
      0: "latitude"
      1: "longitude"
    }
  }
  events {
    timestamp: "UTC timestamp"
    description: "my description"
    user: "id of user"
  }
  users {
    // User data
  }
}

I query all the locations corresponding to the region displayed on the screen, then I query the events which occurred less than 45 mins ago. I do this this way:

regionQuery = geoFire.queryWithRegion(mapView.region)
regionQuery.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in

// I query my events based on the key here

})

The problem with this method is that, for a specific region, I query ALL the locations of events that occurred in the past (which can be a LOT), and THEN I query the ones which happened less than 45 min ago using tradionnal Firebase queries.

Is there a better solution for my problem, cause I'm afraid I'll cross the bandwidth threshold pretty quickly if each time a user moves the map, all the locations of the region displayed are loaded...


Solution

  • Just split the "recent locations" from the "historic locations":

    recent_locations
       g
          l {
            0: "latitude"
            1: "longitude"
          }
    historic_locations
       g
          l {
            0: "latitude"
            1: "longitude"
          }
    

    Now you can run geo-queries against either recent locations or historic locations.