我想知道是否可以通过数据库规则或服务器端删除firebase数据库中的项目。现在我正在使用客户端应用程序在达到计时器后从firebase中删除值。我想避免使用客户端删除项目,因为我不希望多个用户尝试从数据库中删除项目。
其次我想知道是否有办法在firebase中进行独特的观察者查询,我只获取新数据而不是查询所有旧数据。我设置了一个地图,在添加内容时,它会查询该数据并将其添加到地图中。但它会从数据库中提供所有数据,并在我的mapview上添加多个注释。现在我可以在添加注释之前删除注释,但这非常hacky而且不是用户友好的。
代码:
class FBService {
static let instance = FBService()
var firRef: FirDatabaseReference!
private init() {
self.fireRef = FIRDatabase.database().reference()
}
fun getMyAnnotations(location: CLLocation, completion: ([annotationLocation]? -> ())) {
let now = NSDate().timeIntervalSince1970
let locationRef = fireRef.child("annotations").queryOrderedByChild("timestamp").queryStartingAtValue(now)
// Before I was doing fireRef.Child("annotations").queryOrderedByChild("latitude")
locationRef.observeEventType(.Value) { (snapshot) in
completion(self.someMethod(snapshot))
}
}
func someMethod(snapshot: FIRDataSnapshot) {}
}
class MainVC: UIViewController {
override func viewDidLoad() {
FBService.instance.getMyAnnotations(userLocation) { (annotations) in
if let annotations = annotations {
// Before I was doing mapView.removeAnnotations(mapView.annotations), which removes annotations and re-plops them as they come back.
for anno in annotations {
let pin = CustomAnnotation(coordinate: CLLocationCoordinate2DMake(anno.location.latitude, anno.location.longitude), name: anno.name)
mapView.addAnnotation(anno)
}
}
}
}
}