Michael
Michael

Reputation: 303

How to get values from documentSnapshot

I use Firestore and have a collection with a field. The field has multiple String values:

Field: names

With

let documentData = documentSnapshot.data()

I get a dictionary with 1 key (names) and 3 values.

What is the best way to get a String array with the values? (maybe with Codable)

I tried it with

if let data = try? JSONSerialization.data(withJSONObject: documentData as Any, options: []) {
   let users = try? JSONDecoder().decode(??.self, from: data)                
}

Thanks in advance

Upvotes: 0

Views: 1488

Answers (2)

Michael
Michael

Reputation: 303

I solved it now that way. Probably there exists something better.

    let db = Firestore.firestore()
    let userReference = db.collection("users").document(user.uid)

    userReference.getDocument { (documentSnapshot, error) in
        if let error = error {

        } else if let documentSnapshot = documentSnapshot, documentSnapshot.exists {
            if let documentData = documentSnapshot.data() {

                let data = documentData.compactMap({ (arg) -> [String]? in
                    let (_, value) = arg
                    var array = [String]()

                    if let values = value as? [String] {
                        for item in values {
                            array.append(item)
                        }
                    }

                    return array
                })

                let users = data.flatMap({ (value) -> [String] in
                    return value
                })

                print(users)
            }

        } else {

        }
    }

Upvotes: 1

Putte
Putte

Reputation: 328

I'm not sure my way is the best one, but let's give it a try.

I would have done something like this:

//Choosing collection
            db.collection("YOUR_COLLECTION").getDocuments()
                    { (QuerySnapshot, err) in
                        if err != nil
                        {
                            print("Error getting documents: \(String(describing: err))");
                        }
                        else
                        {
                            //For-loop
                            for document in QuerySnapshot!.documents
                            {
                                //let document = QuerySnapshot!.documents
                                let data = document.data()

                                let data1 = data["YOUR_DATA1"] as? String
                                let data2 = data["YOUR_DATA2"] as? String

                                //Now you can access your data1 and data2 like this:
                                //example code:
                                txtfield_data1.text = data1
                                txtfield_data2.text = data2
                            }

And there you can format it however you want. I hope that is what you mean, otherwise, let me know and I'll try to edit it as good as I can. :) Have a good day!

Upvotes: 0

Related Questions