0
votes

we recently started developing an uber like app for iOS using Firebase Firestore and we need to store and constantly update the user's location on firebase and make it so that the other clients can retrieve it as well.

We are trying to store the user location in Users own document on firestore database, such like:

Collections > Users > "AutoGeneratedDocument" > UserLocation with two key values latitude and longitude.

We've made some progress with the code, it writes the location data when we explicitly input a document path to it but we still don't know how to run a query and find the user's own document.

In order to save the location data to the users own document we need to be able to get the auto generated user document path for the currently signed in user.

So far we've come up with this:

func updateUserLocation() {
    let db = Firestore.firestore()
    let locman = CLLocationManager()
    locman.requestWhenInUseAuthorization()
    var loc:CLLocation!
    if CLLocationManager.authorizationStatus() == .authorizedWhenInUse || CLLocationManager.authorizationStatus() == .authorizedAlways{
        loc = locman.location
    }
    let lat:Double = loc.coordinate.latitude
    let long:Double = loc.coordinate.longitude
    let geo = GeoPoint.init(latitude: lat, longitude: long)

    let currentUID = currentUserUID
    let val = db.collection("users").whereField("uid", isEqualTo: currentUserUID)

    db.collection("users").document(val).updateData(["currentUserLocation" : geo])

}

We expect to get the path to the document which contains the signed in user's UID then store the user location key values in that specific document. But with our code, in the last line the document(val) part expects a string but our output with the "whereField" method apparently is a query.

1

1 Answers

0
votes

You're going through users twice:

db.collection("users").whereField("uid", isEqualTo: currentUserUID).updateData(["currentUserLocation" : geo])

You can find many useful tips, tutorials on the official website. Perform simple and compound queries in Cloud Firestore