Was using relamquad's answer quite successfully until I noticed it can lead to memory leaks due to strong reference cycles.
Using it like described works fine:
Swift
var body: some View {
SomeView().uiKitOnAppear {
print("I am uiKitAppear")
}
}
However if you pass a ViewModel's function for example, the ViewModel will not be deallocated once the View should be deinitialized:
Swift
@ObservedObject var viewModel: ViewModel
var body: some View {
SomeView().uiKitOnAppear {
viewModel.startUpdating()
}
}
If been trying to find a solution using weak references, but then again, somehow startUpdating is not called on .uiKitOnAppear().
Does anybody know a solution to this?
Post
Replies
Boosts
Views
Activity
Thanks gfdgdfg for the quick response and solution proposition.
In my specific case, it did not solve the memory leak issue. What helped was resetting the action in updateUIViewController(_ controller: UIAppearViewController, context: Context).
Extending relamquad's answer like this:
swift
struct UIKitAppear: UIViewControllerRepresentable {
let action: () - Void
func makeUIViewController(context: Context) - UIAppearViewController {
let vc = UIAppearViewController()
vc.action = action
return vc
}
func updateUIViewController(_ controller: UIAppearViewController, context: Context) {
controller.action = action
}
}