Geofire / FireBase .indexOn

时间:2015-12-09 23:02:30

标签: ios swift firebase geofire

我正试图从iOS应用程序中使用Geofire。 但我对规则定义失败了。

这就是我的数据集:

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
       }
    }
  }
}

从我的Swift Code中,我使用了这个:

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)

我设置了这样的观察者:

_ = 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")
    })

我使用GeoFire方法为用户设置位置,如下所示:

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

但是我收到了这条消息 [Firebase]使用未指定的索引。考虑将“.indexOn”:“g”添加到/提供给您的安全规则以获得更好的性能

我为规则尝试了几种组合..

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

我是否必须查看我的架构?或者我会错过什么? 有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

我认为你在寻找:

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