2

I'm trying to save the geofire information under each postId but currently not quite sure how to do so. Could someone please help me figure out how to save the information under the postId node.

        @objc func handlePost() {

    navigationItem.rightBarButtonItem?.isEnabled = false

    guard let uid = Auth.auth().currentUser?.uid else { return }

    guard let caption = textView.text, caption.characters.count > 0 else { return }

    let userPostRef = Database.database().reference().child("posts").child(uid)

    let ref = userPostRef.childByAutoId()

    guard let locationName = locationNameButton.titleLabel?.text else { return }

    let latitude = lat
    let longitude = long

    let geoLatitude = (latitude as! NSString).doubleValue
    let geoLongitude = (longitude as! NSString).doubleValue

    geoFireRef = Database.database().reference().child("posts").child(uid)
    geoFire = GeoFire(firebaseRef: geoFireRef)

    let values = ["caption": caption,"locationName": locationName, "latitude": latitude,"longitude": longitude,"creationDate": Date().timeIntervalSince1970] as [String : Any]

    geoFire?.setLocation(CLLocation(latitude: geoLatitude, longitude: geoLongitude), forKey: ref.key!)

Post and UID node

  • What's the problem with the code you shared? – Frank van Puffelen Nov 09 '18 at 03:39
  • Nothing sorry, I'm just trying to figure out how to save my geoFire data under the post node for each post and I'm not sure how to go about saving it under the random `postId` generated. I would appreciate any help sir. –  Nov 09 '18 at 03:49
  • 1
    Use the same id as this one generated here: `let ref = userPostRef.childByAutoId()` for saving geoFire location – DionizB Nov 09 '18 at 08:50
  • @DionizB I'm doing that now and it's working. But now It's being saved twice. Once under the postId which is what I want. But also now a uid node is being created and being saved under that too. Could you tell me where I went wrong sir. –  Nov 09 '18 at 09:12
  • Is it working properly now or not? – DionizB Nov 09 '18 at 09:16
  • @DionizB Yes, but it's now being saved twice. Check the update code and photo. Please. –  Nov 09 '18 at 09:19
  • Try deleting the data and reenter it, and see if the same thing happens again, because it shouldn't add twice now. – DionizB Nov 09 '18 at 09:39
  • @DionizB I just tried. It just over wrote the uid with the new data. not sure why its adding it twice... –  Nov 09 '18 at 09:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183354/discussion-between-dionizb-and-james-jamison). – DionizB Nov 09 '18 at 09:44
  • HaHa, So sorry sir, I accidentally had another `setLocation` in my controller from a previous attempt. Thanks heaps for the help. –  Nov 09 '18 at 09:46

1 Answers1

0

If you want to store the geolocation under the same key as

let ref = userPostRef.childByAutoId()

You can simply get the key property from ref. So:

geoFire?.setLocation(CLLocation(latitude: geoLatitude, longitude: geoLongitude), forKey: ref.key)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • This worked. Although it's now being saved twice once under the postId (Thank you) but now under the user uid too. do you know why? (I added a photo of the database in the updated question.) –  Nov 09 '18 at 06:49
  • The way you initialize GeoFire looks very weird: ` geoFireRef = Database.database().reference().child("posts").child(uid) geoFire = GeoFire(firebaseRef: geoFireRef) `. This means that you have a `GeoFire` object for each user. It also means that you can only geoquery for the an individual user. I'm not really sure I understand what you're trying to do here, and suspect I'm looking at a [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Instead of describing the data structure you want, can you explain what you're trying to accomplish with this code? – Frank van Puffelen Nov 09 '18 at 13:58
  • You're right, that code is only query the current users posts instead of all the posts. I'm just not quite sure how to query all the posts. –  Nov 11 '18 at 23:44
  • You can't query across multiple levels. If you want to geoquery all posts, you should create a location where you have a single-level list of all post keys and their geohash+location. – Frank van Puffelen Nov 12 '18 at 02:53
  • Is there a question where this is answered before? Or an example/documentation I could look at? –  Nov 12 '18 at 04:35
  • https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Frank van Puffelen Nov 12 '18 at 04:40