0

I want to know if it is possible to delete an item in the firebase database through database rules or something server side. Right now I am using client side app to delete values from firebase after a timer is reached. I want to avoid using client side for deletion of the item as I do not want multiple users to try to delete the items from the database.

Second I want to know if there is way to make a unique observers query in firebase where I only get new data instead of querying all the old data. I have it setup with a map where as something is added it queries for that data and adds it to the map. However it is giving me all of the data from the database and causing multiple additions of annotations on my mapview. Now I can remove the annotations before they are added, but that is very hacky and not User friendly.

Code:

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

}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
mn1
  • 519
  • 1
  • 5
  • 15
  • There is no way to run your code on Firebase's servers at the moment. You can typically secure things through security rules. If you give some example of your data structure and the code, we may be able to help with that. – Frank van Puffelen Aug 26 '16 at 13:27
  • Retrieving only new data has been covered a few times. There is no dedicated API for it. A common way is to [discard the initial data](http://stackoverflow.com/questions/18270995/how-to-retreive-only-new-data). The better way is to [use a query to only retrieve the data from "now"](http://stackoverflow.com/questions/33885059/how-to-only-get-new-data-without-existing-data-from-a-firebase), since that prevents retrieving data you don't need. – Frank van Puffelen Aug 26 '16 at 13:31
  • Hey Frank, thanks for your input. So the data structure is like this annotation: { timestamp: { timestamp, id, description }}. I tried using queryWithChild("timestamp").queryStartingAtValue(now). However doing a query with startingAtValue will only give values equal or greater than that value. In the beginning I will need the values that are older and then after that only track new values that are changed. – mn1 Aug 26 '16 at 19:08
  • It sounds like you have some code already that doesn't work. It will be a lot easier to help if you show the [minimal code that reproduces the problem you have](http://stackoverflow.com/help/mcve). – Frank van Puffelen Aug 26 '16 at 20:17
  • I created a gist of basically what I tried before and after. https://gist.github.com/miwand/3661d0d90f6b9552d85271b2d7e79f19. This wont work as it will give me new changes and not annotations that were added previously before the date. Thanks again – mn1 Aug 26 '16 at 21:18

0 Answers0