Place Firestore query result into an Array

I am new to programming and cannot find out how to place the result of a firestore query into an Array so that I can use the Array in a pickerview. Here is my code so far. The name of the Array is data.
let db = Firestore.firestore()
db.collection(“users”).document(“user”).collection(“Horses”).whereField(“mustangName”, isEqualTo: true).getDocuments() { (querySnapshot, error) in
if let error = error {
print(“Error getting Documents: (error)”)
} else {
for document in querySnapshot!.documents {
print("(document.documentID) => (")

This is where I am stuck?

Any advice would be very helpful, Thanks

is your array an object that closely represents your “horses” database?

you may need to create a horses class so you will be able to create an array of “horses”

which will then be used to populate the array

Thanks for the reply Francis. I have acollection with many documents in firestore that represents many horses, I need to create a pickerview (which I have no problem with) so that the user can select which horse they want to work with, the list of horses needs to go into an array (only their horses) so that it can populate the pickerview. So the user is already logged in and want to add some details about one of their horses and I thought this would be the best way to enable the user to choose the horse they are working with.

Assuming you want array of horse strings. You would add this code.
in declarations section

var horses = [String]()

add this directly below you existing code

//get horse from db query
let horse = document.get("horse") -- "horse" or whichever database field name represents your horse

//add it to your array

as Francis suggested you may need to create a class or struct if your horse is an object

struct Horse {
  var horsename: String
  var horsetype: String
  var ,,,,,

var horses = [Horse]()

Then you would add this code:

//get data from db query
let horsename = document.get("horsename")  -  inside the strings is the name from your database
let horsetype = document.get("horsetype")  

//create a horse
let horse = Horse(horsename: horsename, horsetype: horsetype)

//add it to your array
1 Like

Thanks very much for the response, I appreciate it a lot! I have now figured it out, I also had to add ‘self.selectPicker.reloadAllComponents()’ for my picker to get the data from the array.
Whew, it was quite a battle:)

1 Like

good job on making it this far, programming is long and hard but very rewarding afterwards isn’t it :slightly_smiling_face: