Hi,
I just started playing around with SwiftUI and have stumbled over something: in my "regular" apps, my view controllers take care of loading data from a web service (triggered by a function called in the ViewDidLoad() part) and then hand the information over to the views.
If I understand the concept of SwiftUI correctly, this whole piece of loading data is no longer managed in the View files but somewhere outside.
So in my app now I have a class of WarehouseOrder and created another class called WarehouseOrderController that is responsible for loading the data from the server and storing it in an array (happens in function loadData() called by the init() of that class)
@Published var warehouseOrders:[WarehouseOrder] = []
From my main menu I call the View WarehouseOrderOverview via a NavigationLink
struct WarehouseOrderOverview: View {
@EnvironmentObject var settingStore: SettingStore
var warehouseOrderController = WarehouseOrderController()
var body: some View {
List(warehouseOrderController.warehouseOrders){ warehouseOrder in
Text(warehouseOrder.orderRef)
}
}
}
What I don't get is that when I start the app, it is triggering the init() of WarehouseOrderController already when I am in the main menu. How can I change it that the data only gets loaded when I actally trigger the NavigationLink to WarehouseOrderOverview?
Max