Stan92
Stan92

Reputation: 453

Geofire / FireBase .indexOn

I m trying to figure out to use Geofire from an iOS app. But I failed about the rules definition.

This is how my dataset that looks like :

offers:{ 
   user1:{
     name : "Steph",
     position : { 
         g:"xxxx",
         l: {
              0: 48.8795522,
              1: 2.3568256
         }
     }
  },
  user2:{
    name:"John",
    position:{ 
       g:"yyyy",
       l: {
          0: 48.8795528,
          1: 2.3568256
       }
    }
  }
}

From my Swift Code, I use this :

let geoRef = Firebase(url: appDelegate.fireBaseUrl + "/offers"  )
let geoFire = GeoFire(firebaseRef: geoRef)
let center = CLLocation(latitude: 48.8795528, longitude:2.3568256)
let myQuery = geoFire.queryAtLocation(center, withRadius: 0.6)

And I set the observers like this :

_ = myQuery.observeEventType(GFEventTypeKeyEntered, withBlock: { (key: String!, position: CLLocation!) in
      print("Key '\(key)' entered")
    })
_ = myQuery.observeEventType(GFEventTypeKeyMoved, withBlock: { (key: String!, position: CLLocation!) in
      print("Key '\(key)' moved")
    })
_ = myQuery.observeEventType(GFEventTypeKeyExited, withBlock: { (key: String!, position: CLLocation!) in
      print("Key '\(key)' exited")
    })

And I use the GeoFire method for setting the location for the user like this :

geoFire.setLocation(CLLocation(latitude: myAdresse.Latitude, longitude: myAdresse.Longitude), forKey: "position")

But I get this message
[Firebase] Using an unspecified index. Consider adding ".indexOn": "g" at /offers to your security rules for better performance

I tried several combinations for the rules..

{
  "rules": {
  "offers":{
    ".read": true,
    ".write": true,
    "$users": {
        "position":{
        ".indexOn":"g"
        }
     }
   }
 }
}

Do I have to review my schema? Or do I miss something? Any help ?

Upvotes: 1

Views: 1462

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598708

I think you're looking for:

{
  "rules": {
  "offers":{
    ".read": true,
    ".write": true,
    "$users": {
      ".indexOn":"position/g"
     }
   }
 }
}

Upvotes: 1

Related Questions