Init or body ?

I have a store, but need to call the method inside the body. However the only way it works is if I call it inside init otherwise I have an error.

@ObservedObject var store = MyStore()
init(category: categoryModel) {
    store.getData(categoryId: category.id)  //here works
}

var body: some View {
        store.getData(categoryId: category.id) //Here I will have the error: Type '()' cannot conform to 'View'

Why ? Thx

Answered by OOPer in 688499022

but need to call the method inside the body. 

Can you explain why you think so? In almost all cases, you have no need to (and should not) do something in body. You may be mistaking something.

Have you tried onAppear {...} ?

Accepted Answer

but need to call the method inside the body. 

Can you explain why you think so? In almost all cases, you have no need to (and should not) do something in body. You may be mistaking something.

Have you tried onAppear {...} ?

Init or body ?
 
 
Q