0

I need a function that returns an array, so I wrote this:

fileprivate func fetchAlbumSongs(_ id: String) -> [AlbumSong]{
        var albumSongs: [AlbumSong] = []        
FirebaseManager.shared.firestore.collection("albums").document(id)
.collection("songs").getDocuments { querySnapshot, error in
            if let error = error {
                print(error.localizedDescription)
                return
            }
            guard let documents = querySnapshot?.documents else {
                return
            }
            documents.forEach { document in
                let data = document.data()
                let artist = data["artist"] as? String ?? ""
                let duration = data["duration"] as? Int ?? 0
                let image = data["image"] as? String ?? ""
                let title = data["title"] as? String ?? ""
                let track = data["track"] as? String ?? ""
                albumSongs.append(AlbumSong(title: title, duration: TimeInterval(duration), image: image, artist: artist, track: track))
            }
        }
        return albumSongs
    }

But this leads to returning nothing since return gets called before the items are appended in the array.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
AdamLeet
  • 127
  • 8

0 Answers0