I've created a document which contains an array within Firestore.
I now want to store that array locally within my app
I have create an array manually, But how can I populate it with the data from my Firestore document?
//Manually created Array
var devices = [ "Laptop",
"Tablet",
"Smartphone",
"Smartwatch",
"Games Console"]
override func viewDidLoad() {
super.viewDidLoad()
createDevicePicker()
createToolbar()
getPickerData()
}
//Function to pull data from Firestore
func getPickerData() {
let docRef = firebaseDB.collection("devices").document("types")
docRef.getDocument { (document, error) in
if let document = document, document.exists {
//This will print out the data
var test = document.data()
print(test)
} else {
print("Document does not exist")
}
}
}
document.data()
comes in as [String: Any]
.
As long as it's an Array of Strings in your Firestore
let array = document.data()["devices"] as! [String]
print(array)
UPDATE TO QUESTION
How to append to the already created devices array.
var devices = [ "Laptop",
"Tablet",
"Smartphone",
"Smartwatch",
"Games Console"]
let array = data["sub_category"] as! [String]
devices += array
print(devices)