UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.