0

So I have this code, where I fetch a url from firestore and then append it to an array, which is then stored in userDefaults(temporarily).

In the view I basically just iterate over the array stored in userdefaults and display the images.

But the problem is, that I have to rerender the view before the images show. How can i fix this?

struct PostedImagesView: View {
   
    @State var imagesUrls : [String] = []
    
    @ObservedObject var postedImagesUrls  = ProfileImages()
    
    
   
    var body: some View {
        VStack{

            ScrollView{

                ForEach(postedImagesUrls.postedImagesUrl, id: \.self) { url in
                    ImageWithURL(url)
                }

            }
            
        }
        .onAppear{
                GetImage()
                print("RAN GETIMAGE()")

            
        }
    }
    
    // Get Img Url from Cloud Firestore
    func GetImage() {
        guard let userID = Auth.auth().currentUser?.uid else { return }
        
        let db = Firestore.firestore()
        
        db.collection("Users").document(userID).collection("PostedImages").document("ImageTwoTester").getDocument { (document, error) in
            if let document = document, document.exists {
                
                // Extracts the value of the "Url" Field
                let imageUrl = document.get("Url") as? String

                UserDefaults.standard.set([], forKey: "postedImagesUrls")
                imagesUrls.append(imageUrl!)

                UserDefaults.standard.set(imagesUrls, forKey: "postedImagesUrls")
                
            } else {
                print(error!.localizedDescription)
            }
        }
        
    }
}
Nick
  • 219
  • 4
  • 15
  • There is 3 things in your View 1- ForEach, 2-ScrollView, 3-VStack, all those follow the ForEach, ForEach is the only child here, who order to parents what is going on, and ForEach works when it got something to Work, I mean urls! So you are asking for impossible thing, Rendering this View is under control of ForEach, give url to ForEach it will render, take all urls from it, it will render nothing. – ios coder Mar 03 '21 at 23:24
  • Maybe I wasn’t very clear before, but the problem is that the data isnt stored in userDefaults until the second time the view renders. And i want it to be stored right away @swiftPunk – Nick Mar 03 '21 at 23:32
  • So you are talking about **GetImage()** which happens at **onAppear**, and you want that happens even before **onAppear**, right? – ios coder Mar 03 '21 at 23:43
  • Idk if thats what I want, what im saying is that when the view appears GetImage() is run, but the userDefault is first set the second time the view renders. So im not sure I want what you are saying, but how do i run GetImage() even before onAppear? @swiftPunk – Nick Mar 06 '21 at 17:38
  • Does this answer your question? [How can I use UserDefaults in Swift?](https://stackoverflow.com/questions/31203241/how-can-i-use-userdefaults-in-swift) – Andrew Apr 19 '21 at 09:23

0 Answers0