TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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)")
}

2      

@aarku  

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.

2      

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.

2      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.