'Grabbing other element's from array firestore
I am needing help with grabbing certain details about an audio uploaded to my Firestore Database. This is how the data is entered in my document for the audio : 
It can take in multiple audio urls with information and store them in the array of AudioUrl. I originally had it to where it just took in the audio url with no additional information except the name but it was structured like this wowza:"https://firbase......" and would be able to fetch the key as the audio name and the value as the audio file. Ever since I have changed the structure of the data to allow additonal information for each audio url uploaded, I have been having a hard time figuring out to actually fetch the rest of the data. This is my current api call to grab the data from firestore:
guard let documents = snapshot?.documents else { return }
for document in documents {
let songID = document.documentID
let data = document.data()
let uid = data["uid"] as! String
let photoURL = data["photoURL"] as? String
let trackTitle = data["trackTitle"] as? String
let url = URL (string:photoURL!)
let coverImageURL = data["coverImage"] as? String
let imageURL = URL (string:coverImageURL!)
let audios = data["AudioUrl"] as! [[String:Any]]
var audioNames = [String]()
var audioUrls = [URL]()
let timestamp = data["timestampt"] as? Double
for audio in audios{
let audioName = audio.first?.key
let audioUrl = audio.first?.value as? String
let songURL = URL (string:audioUrl!)
audioNames.append(audioName!)
audioUrls.append(songURL!)
UserService.shared.fetchUser(uid: uid) { user in
let song = SongPost(id: songID, author: user, title: trackTitle!, coverImage: imageURL!, audioUrl: audioUrls , audioName: audioNames , likes: 0 , timestamp: timestamp!, streams: 0)
self.songs.removeAll(where: {$0.id == songID})
self.songs.append(song)
self.songs = self.songs.sorted(by: { $0.createdAt > $1.createdAt})
self.collectionView.reloadData()
}
}
}
The for loop where it starts as for audio in audios is where I fetch the information about the audio url stored in the array. I'm sure there is a way to splice the array or grab each element by index but I am sadly stuck on this. Thank you in advance!
Solution 1:[1]
The Firestore syntax is straightforward:
for doc in documents {
if let audioUrl = doc.get("audioUrl") as? [[String: Any]] {
for url in audioURL {
if let name = url["Name"] as? String {
print(name)
}
}
}
}
This would be a good time to mess around in an Xcode playground. AudioURL is an array of dictionaries (called maps in Firestore) so just create an arbitrary array of dictionaries and get the hang of them. Plug this into a playground:
import Foundation
let audioURL: [[String: Any]] = [
["File": "abc123", "Likes": 0, "Name": "Wowza"],
["File": "xyz456", "Likes": 1, "Name": "Yowza"]
]
for url in audioURL {
if let file = url["File"] as? String,
let likes = url["Likes"] as? Int,
let name = url["Name"] as? String {
print(file, likes, name)
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
