1

I am trying to save an object that contains a name string, an address string and location coordinates. In the docs it seems that there is one way to save firebase data and another way to save GeoFire data.

Firebase:

  var alanisawesome = ["full_name": "Alan Turing", "date_of_birth": "June 23, 1912"]
  var gracehop = ["full_name": "Grace Hopper", "date_of_birth": "December 9, 1906"]
  var usersRef = ref.childByAppendingPath("users")
  var users = ["alanisawesome": alanisawesome, "gracehop": gracehop]
  usersRef.setValue(users)

GeoFire:

  let geofireRef = Firebase(url: "https://<your-firebase>.firebaseio.com/")
  let geoFire = GeoFire(firebaseRef: geofireRef)
  geoFire.setLocation(CLLocation(latitude: 37.7853889, longitude: -122.4056973), forKey: "firebase-hq") { (error) in
      if (error != nil) {
         println("An error occured: \(error)")
      } else {
         println("Saved location successfully!")
      }
  }

Is it possible to save both location and other data in the same request? I'd like to do it in the same request because I don't want the user to be able to create an object without location data if the location request fails. Or is there another smart way to impose that restriction?

user2634633
  • 509
  • 8
  • 21

2 Answers2

0

Since writing the original entity and its geolocation are two separate calls, they will be two separate write operations. One of these writes is done by your code, the other is done by GeoFire. Both calls write to different parts of the JSON tree.

Firebase recently added the ability to write to multiple locations with a single update() call. With that you could write both the geolocation and the entity in one call. If you want to do that, you'll have to change GeoFire to allow for it.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I'm using the multiple updates call elsewhere in the code, so that wouldn't be a problem. What do you mean by changing GeoFire? – user2634633 Feb 07 '16 at 16:35
  • To allow the writes from your application and from GeoFire to happen in a single `update()` call will require changes to the code of both your application code and to GeoFire. Since GeoFire is open-source, you can clone/fork the repo and make those changes yourself. – Frank van Puffelen Feb 07 '16 at 16:52
  • Ah, that seems like overdoing it, I can work around it. It would be great to see such feature in the future thought! – user2634633 Feb 07 '16 at 18:05
  • It sure would. Feel free to add a feature request to the repo. – Frank van Puffelen Feb 07 '16 at 18:36
  • @FrankvanPuffelen Hi, I'm trying to pull this off as well at the moment, if you have time - would you mind checking out my [question](http://stackoverflow.com/questions/36505252/geofire-update-in-same-call) real quick? – Erik Apr 08 '16 at 18:21
0

I also struggled for while finding a solution to this, I ended up taking an unorthodox approach. I make my own key title for the GeoFire key entered. Almost like a vin for a car. The first character is a number defining my annotation to use. Next set of numbers is date generated at time of post, after the date is a string of user data because it doesn't matter how long that text is. And that goes as the key. In my maps app the title key is then called and I retrieve the title as a string and cut it up. There are certain characters that are not allowed to be in the GeoFire key title, so you have to restrict the user from typing those.