I have an iOS project written in SwiftUI but the navigation is handled with UIKit. Therefore each view has its own UIHostingController which takes care of initializing the view written in SwiftUI within its init(), for example:
final class ExampleViewController: UIHostingController<ExampleView> {
required init?(coder: NSCoder) {
super.init(coder: coder, rootView: ExampleView(viewModel: .init(model: nil)))
}
init(model: ExampleModel) {
super.init(rootView: ExampleView(viewModel: .init(model: model)))
}
}
Where ExampleView is something like:
struct ExampleView: View {
@StateObject var viewModel: ExampleViewModel
var body: some View {
VStack {
// Content
}
}
}
My doubt arises when I have to share data that can be updated from different views, I'll give a practical example:
I have a view that shows a list of tasks and a view that shows the detail. In the task detail user can change, for example, the name and status of the task (todo, inProgress and completed) of the element and and when these are modified should also change in the list without updating the data via APIs.
How can I achieve this without using callbacks, delegates or notifications but taking advantage of the tools available with SwiftUI? Is it possible even if navigation is managed with UIKIT?
I did several tests by sharing the @Published data array between the view models but I don't understand why the changes are not reflected in the UI of the list although the data is updated, so I was wondering if it was possible.