How do you guys deal with @ObservedObject in iOS 13? I'm having issues with this because every change on state recreates my ViewModels and I can't use @StateObject because it's only available on iOS 14 and I have to support iOS 13 users.
When the subview changes/re-renders, your ViewModel stay the same on the parent view and is not recreated as it is a @State; and your subview can still listen to @PUublished changes.
We've made a generic wrapper for it, and we use almost everywhere:
struct ViewModelWrapper<V: View, ViewModel: ObservableObject>: View {
private let contentView: V
@State private var contentViewModel: ViewModel
init(contentView: @autoclosure () -> V, vm: @autoclosure () -> ViewModel) {
self._contentViewModel = State(initialValue: vm())
self.contentView = contentView()
}
var body: some View {
contentView
.environmentObject(contentViewModel)
}
}