Save a CLLocationCooridnate2D array in Firestore

2019-03-06 21:03发布

问题:

I'm trying to save a CLLocationCoordinate2Darray into Firestore, but I'm getting error:

'FIRInvalidArgumentException', reason: 'Unsupported type: NSConcreteValue'

And I don't really know how I can perform this.

In the top of the code I've this:

var locations: [CLLocationCoordinate2D] = []

And down in the 'save' button. I've this code:

//Document Reference properties
        var _: DocumentReference = self.db
            //Collection into Subcollection & Documents
            .collection("rastad").document(authentication!)
            .collection("promenad").addDocument(data:
                //Insert first values
                ["Dog": txtfield_dog.text!,
                 "Person": txtfield_person.text!,
                 "What": txtfield_what.text!,
                 "Date": txtfield_Datum.text!,
                 "Time": txtfield_Time.text!,
                 "Timer": txtfield_timer.text!,
                 "Kilometers": txtfield_km.text!,
                 "Locations": locations
                ], completion: { (err) in
                    if err != nil
                    {
                        print("Error adding info-details")
                        return
                    }
                    else
                    {
                        print("Succeded!")
    }

So, how can I solve this issue? How can I insert the CLLocationCoordinate2D array into Firestore? And also, how can I later on retrieve it? I would really appreciate any help! Thanks!

EDIT: Maybe this error is clear for you, but well. It's not clear for me and thats the reason why I'm asking the question of course. If I'd know the answer I would not answer this question. And instead of disliking and leave the thread, you can at least leave a comment and tell me the reason for the dislike. So, I guess my question will stand.

回答1:

To break it down, Firebase doesn't know what a CLLocationCoordinate2D object is. But it does know what an array is, and it does know what strings and integers and doubles are. So you will have to parse your array of locations into something it can understand. You can store the value in Firestore since a coordinate is really just a pair of double values. You can do this a number of ways, but the easiest is to probably split the locations array, one for the latitude values and one for longitude. For example, you can use let latitudes = locations.map({ $0.latitude }) and the same for longitudes. Then in Firestore, you would update it to something like:

                 "Person": txtfield_person.text!,
                 "What": txtfield_what.text!,
                 "Date": txtfield_Datum.text!,
                 "Time": txtfield_Time.text!,
                 "Timer": txtfield_timer.text!,
                 "Kilometers": txtfield_km.text!,
                 "LocationLatitudes": latitudes,
                 "LocationLongitudes": longitude,
                ]

Apply reverse logic for retrieving. You will need to iterate over the lat & long arrays and combine into a single array of CLLocationCoordinate2D objects.