e.iluf
e.iluf

Reputation: 1659

swift: how to call a function in Observableobject

I just picked up swift programming and I am trying to call a function when a view is loaded. For example I want to call getValue() when the searchResult view is display so that getValue can populate the array i need in the view.

class SearchResultViewModel: ObservableObject {
      
    var searchResults:[Any] = [] // ->Creates an instance of the struct object
    var aisleArry: [Int] = []
     init() {
        self.getValue()
      }
    
    func getValue(){
        
        print("We have activated searchModel")
        for (productId , productVal) in Global.productDict {
            let aisle = productVal.aisleNo
            let name  =  productVal.name
            let aislezone = productVal.location_zone
            let aislesect = productVal.location_section
            let price = productVal.productPrice

            if aisleArry.contains(aisle){
                print("already in aisle array")
                
            } else {
                aisleArry.append(aisle)
                
            }
            
        }
    }
 }

here is the searchResult view that uses the model above

struct SearchResultView: View {
    @ObservedObject var model: SearchResultViewModel
   
    
        var body: some View {
 
            VStack {
                VStack{
                  
                    HStack{
                        ForEach(0 ..< model.aisleArry.count){aisleNum in
                            Text(String(self.model.aisleArry[aisleNum])).bold()
                            
                        }
 
                    }
                    
                }
                Spacer()
            }
            
        }
    }

after playing around with many methods, the last i tried was self.getValue which does not work. How can I call functions in the view model when the view is loaded?

The getValue function is not being called when the searchResultView is called. How do I call the getValue function when searchResultView is displayed?

Upvotes: 2

Views: 2310

Answers (1)

Asperi
Asperi

Reputation: 258285

Call it on appear

struct SearchResultView: View {
    @ObservedObject var model: SearchResultViewModel
    
        var body: some View {
 
            VStack {
                VStack{
                  
                    HStack{
                        ForEach(0 ..< model.aisleArry.count){aisleNum in
                            Text(String(self.model.aisleArry[aisleNum])).bold()
                            
                        }
                    }
                }
                Spacer()
            }
            .onAppear { self.model.getValue() }  // << here !!
        }
    }
}

and make model really observable

 class SearchResultViewModel: ObservableObject {
      
    @Published var searchResults:[Any] = []
    @Published var aisleArry: [Int] = []
    
    func getValue(){
        
        print("We have activated searchModel")

        // ... other code
}

Upvotes: 3

Related Questions