将索引添加到Firebase规则

时间:2017-12-30 17:43:34

标签: ios swift firebase firebase-realtime-database geofire

我使用GeoFire根据用户的当前位置查询Firebase中的数据。我得到了:

  

使用未指定的索引。考虑在/来添加“.indexOn”:“g”   您的安全规则以获得更好的性能

在控制台中。以下是我的查询代码:

            ref = Database.database().reference()
            let geoFire = GeoFire(firebaseRef: ref)

            let center = CLLocation(latitude: (userLocation.coordinate.latitude), longitude: (userLocation.coordinate.longitude))

            let circleQuery = geoFire?.query(at: center, withRadius: 1)

            circleQuery?.observe(.keyEntered, with: { key, location in

                guard let key = key else { return }

                geoFire?.getLocationForKey(key, withCallback: { (location, error) in

                    let myAnnotation: MKPointAnnotation = MKPointAnnotation()
                    myAnnotation.coordinate = CLLocationCoordinate2DMake((location?.coordinate.latitude)!, (location?.coordinate.longitude)!);
                    self.currentLocationMapView.addAnnotation(myAnnotation)      
                })


                self.ref.child("services").child(key).observeSingleEvent(of: .value, with: { (snapshot) in

                    let value = snapshot.value as? NSDictionary
                    let servicename = value?["name"] as? String ?? ""
                    let address = value?["address"] as? String ?? ""

                    self.nameArray.append(servicename)
                    self.addressArray.append(address)


                    if (self.nameArray.count > 0) {
                        self.customTableView.reloadData();     
                    }

                })

            }) 

我的Firebase规则标签包含以下设置。

"rules": {
    ".read": true,
        ".write": "auth != null"
          }

无论我在哪里插入“.indexOn”:“g”,我都收到错误,无法保存规则。有什么帮助吗?

1 个答案:

答案 0 :(得分:2)

documentation指向examples

"rules": {
  ".read": true,
  ".write": "auth != null",
  ".indexOn": ["g"]
}