0

New (to swiftui), I would like to display an image(refresh the view) as soon the url is ready .Apparently State var isn t enough . It seems like .onReceive and a publisher might help but i iam helpless . NB : i use KingFisher but it seems to be the same case for any loader .The situation seems special (url not ready when view created) but this is a more general topic: for example when requests are sync and profile photo needs to be updated when the other user changes it .

Edit: After some more tests, it appears that view is updated if ,for example , we use a dispachQueue to postpone for one second and then try to update . It is not updated when retrieving the value directly with firebase onAppear

import SwiftUI
import Firebase
import Kingfisher


struct Test4: View {

    @State private var mProfilePhotoUrl: URL = URL(string:"0")!
 
 var body: some View {
    
 VStack(alignment: .center,spacing : 0){
           
           KFImage(mProfilePhotoUrl)
      
  }.onAppear(){
        setUpProfileElements()
       }
     
    }
    
    private func setUpProfileElements(){

    //    FirebaseRequest -> mProfilePhotoUrl = URL(string: User.profilePhoto)
     
    }
    
}
it75
  • 125
  • 10
  • Seems like that should be KingFisher's responsibility in this case -- I'm surprised it doesn't handle this scenario. What about adding `.id(mProfilePhotoUrl)` to it to force it to change when the URL changes? – jnpdx Mar 31 '21 at 15:58
  • This would update when using a delay but not with a firebase request used on appear. Using Id to force the update made the trick . Others might encourter this case . Thank you ! – it75 Mar 31 '21 at 17:23

1 Answers1

1

This is jnpdx Answer ! I dont know the reason , if it was too close from displaying, but view wouldn t update with the firebase request onAppear, using id foreced the refresh.

struct Test4: View {

    @State private var mProfilePhotoUrl: URL = URL(string:"0")!
    *@State private var ID = 0*

 var body: some View {
    
 VStack(alignment: .center,spacing : 0){
           
           KFImage(mProfilePhotoUrl)*.id(ID)*
      
  }.onAppear(){
        setUpProfileElements()
       }
     
    }
    
    private func setUpProfileElements(){

    //    FirebaseRequest -> mProfilePhotoUrl = URL(string: User.profilePhoto)
     //                    *-> self.ID += 1*
    }
    
}
it75
  • 125
  • 10