WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

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 = Storage.storage().reference()

                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")
                                self.rtrvPh.append(image)
                                print("imageAppended")

                            }
                        }
                    }
                }
            }
        }
    }

    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.

   

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.