NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Not appending in nested for loops

Forums > Swift

I am trying to get data out of a set of nested for loops by appending to a list outside the function. The code below worked in other similar instances pulling different data out. Now though, the data prints to terminal but only "Addition 1" appends to rtrvList on print.

Any thoughts on why?

@Published var rtrvList:[String] = [String]()

func retrievePhoto() {

    let db = Firestore.firestore()

    self.rtrvList.append("Addition 1")

    db.collection("Users").document("4aoBgdvTJIZ82UgY0MiDW6fSluv2").collection("All_Profiles").getDocuments { snapshot, error in
        if error == nil && snapshot != nil {

            self.rtrvList.append("Addition 2")

            var paths = [String]()

            for doc in snapshot!.documents {
                paths.append(doc["H_1ref"] as! String)
                paths.append(doc["H_2ref"] as! String)
                paths.append(doc["H_3ref"] as! String)
                paths.append(doc["H_4ref"] as! String)

            for path in paths {

                let pathRef =

                self.rtrvList.append("Addition 3")

                let fileRef = pathRef.child("4aoBgdvTJIZ82UgY0MiDW6fSluv2/All_Pictures/\(path)")

                self.rtrvList.append("Addition 4")

                fileRef.getData(maxSize: 1 * 1024 * 1024) { data, error in

                    self.rtrvList.append("Addition 5")

                    if error == nil && data != nil {

                        self.rtrvList.append("Addition 6")

                       if let image = UIImage(data: data!) {
                            DispatchQueue.main.async {

                                self.rtrvList.append("Addition 7")


    print("RetrieveList - \(rtrvList)")



You are ignoring any error you get when fetching that first document. You should print out your errors or something.

This needs some serious refactoring, but at least do yourself the favor of adding some kind of error handling. You just need to narrow down where the problem is, be that a debugger, print() calls, in-app UI, anything. Just don't ignore them! They are going to answer your question.


Ok thank you, I appended print statements and they fire all the way down through the stack. As for the refactoring, sorry if it is hard to read, I got it off of the internet as a template and usually customize from there but this was how I found it. I will try to sort it out.


Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.