0

I have a Firebase database with nested dictionaries attached to the Firebase autoId() method.

Snap (donators-items) {
    "-LjGwJLdBoFugIaPMVJg" =     {
        description = "Yummy Pasta";
        latitude = "44.93926644825692";
        longitude = "4.872616600173217";
        name = "Pasta";
        pickUpDate = "2019-07-09T16:15:53+02:00";
        selectedType = "Food";
    };
    "-LjGwWkbOzeXJ6U3JO7r" =     {
        description = "Brand new shirt";
        latitude = "44.93607125515575";
        longitude = "4.878479988801786";
        name = "Cool T-shirt";
        pickUpDate = "2019-07-12T16:15:41+02:00";
        selectedType = "Clothes";
    };
}

I am using this method to retrieve the database dictionary.

var donatorsItems = [DonatorItem]()

func fetchDonatorsItemsFromFirebase() {
  donatorsItems.removeAll()

  rootRef.child(FirebaseRoot.donatorsItems.rawValue).observe(.value) {
    (snapshot) in
    if let idDict = snapshot.value as? NSDictionary {
      let ids = idDict.allKeys
      for key in ids {
        if let nestedDict = idDict[key] as? FirebaseDictionary {

          print(nestedDict)

        }
      }
    }    
  }
}

When printing nestedDict, I populate two nested dictionary in an unordered collection.

How can I retrieve the datas to add it to my DonatorItem object and send it in an order from closer to further using the latitude and longitude of each object?

I have created the function that calculates the distance between the user and the latitude, longitude of the object.

func getDistanceFromUserToMeetingPoint(_ meetingPointLatitude: Double, _ meetingPointLongitude: Double, vc: UIViewController) -> Double
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Roland Lariotte
  • 2,606
  • 1
  • 16
  • 40
  • 1
    Question: You have a function that calculates the current distance between the user and an object. Why don't you load the objects, calculate the distance to each, sort that result ascending? That would result in a list of objects closest to furthest? – Jay Jul 09 '19 at 20:19
  • This is what I was hopping to do, but I can not manage to retrieve the data from each saved object yet. Still trying to figure out how to get the values out of my nested dictionaries and then, I should be able to reorder them after calculating their position from the user. – Roland Lariotte Jul 09 '19 at 21:41
  • 1
    Well, that code is kinda of a mess and using older objects instead of Swifty ones.. I just posted an answer to another somewhat similar question that will get you going. See [this answer](https://stackoverflow.com/questions/56949092/once-coordinates-are-saved-as-string-in-firebase-how-are-they-fetched-in-cllloa?noredirect=1#comment100459542_56949092) – Jay Jul 09 '19 at 21:44

1 Answers1

1

Firebase Database queries can only order/filter on a single property. Since a location is indicated by two properties in your database, there is no way to order/filter on location or distance right now.

There is an add-on library for Firebase called GeoFire, which adds this capability by adding a geohash value to each node. A geohash is a single string value that encodes the latitude and longitude in a way that allows Firebase to filter nodes within a certain range from a given point.

This will allow you to query for items within a range, but you won't get the results from nearest to furthest. This is simply not possible with Firebase, as it has no way to know the "distance" on the server. So you will have to retrieve the items in a certain range, and then order them by distance in the client-side code of your app.

For some more questions on the same topic, see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807