Joel Henry
Joel Henry

Reputation: 31

SwiftUI View Lifecycle, reinitalise

Im Struggling with View lifecycle and swift view reusability.

In this example, my view can be reinitialised with a different id, and must then be tasked with displaying the username asynchronously. I was previously running the loadData function inside the OnAppear, however it would then not reinitialise after the first load. However now that I added the init function, my state & view are not being updated at all on any init.

struct TestView: View {
    
    @State var userName : String  = ""
    
    init(id: Int){
        print("Init \(id)")
        loadData(id: id)
    }
    
    var body: some View {
        VStack{
            if userName != "" {
                Text(userName)
            }
        }
    }
    
    // function that will return username at some point in the future
    private func loadData(id: Int){
        let Obs: Observable<String> = manager.getUser(id: id)
               
       Obs.subscribe(onNext: { user in
            self.userName = user.userName
       })
    }
}

Upvotes: 1

Views: 207

Answers (1)

Asperi
Asperi

Reputation: 258345

View is not rebuild because in provided variant it is interpreted as equal (because no different properties are stored)

Try the following

struct TestView: View {
    let id: Int            // << changing this should make view rebuild
    @State private var userName : String  = ""
    
    var body: some View {
        VStack{
            if userName != "" {
                Text(userName)
            }
        }
        .onAppear {
            print("Rebuild with: \(id)")
            loadData(id: id)
        }
        .id(self.id)     // << Update: this one !!
    }
    
    // function that will return username at some point in the future
    private func loadData(id: Int){
        let Obs: Observable<String> = manager.getUser(id: id)
               
       Obs.subscribe(onNext: { user in
            self.userName = user.userName
       })
    }
}

Upvotes: 1

Related Questions