I have GlobeView() embedded in a SwiftUI view. I want to call a function to execute from the SwiftUI view to the GlobeViewController.
What's the easiest way I can achieve this given this layered view hierarchy?
I know I can pass down a simple closure, but in doing so, I get a bug where SCNParticleSystem is infinitely added to the SceneView since the init fires every time the view updates.
Is there an alternative approach where I can call this function?
Is there a way to detect variable changes in viewModel from GlobeViewController?
I know I can use didset on viewModel, but how would I know when a specific member variable of viewModel is set?
import SwiftUI
import SceneKit
import Foundation
import SceneKit
import CoreImage
import MapKit
class GlobeViewModel: ObservableObject {
@Published var option: Int = 2
@Published var hideSearch: Bool = false
@Published var executeFunction: Bool = false
}
struct MainProfile: View {
@EnvironmentObject var viewModel: GlobeViewModel
var body: some View {
GlobeView() //call function from here
}
}
typealias GenericControllerRepresentable = UIViewControllerRepresentable
@available(iOS 13.0, *)
private struct GlobeViewControllerRepresentable: GenericControllerRepresentable {
@EnvironmentObject var viewModel: GlobeViewModel
func makeUIViewController(context: Context) -> GlobeViewController {
let globeController = GlobeViewController(earthRadius: 1.0, popRoot: viewModel)
return globeController
}
func updateUIViewController(_ uiViewController: GlobeViewController, context: Context) { }
}
@available(iOS 13.0, *)
public struct GlobeView: View {
public var body: some View {
GlobeViewControllerRepresentable()
}
}
public typealias GenericController = UIViewController
public typealias GenericColor = UIColor
public typealias GenericImage = UIImage
public class GlobeViewController: GenericController {
var viewModel: GlobeViewModel
public var earthNode: SCNNode!
internal var sceneView : SCNView!
private var cameraNode: SCNNode!
init(popRoot: GlobeViewModel) {
self.viewModel = popRoot
super.init(nibName: nil, bundle: nil)
}
init(popRoot: GlobeViewModel) {
self.viewModel = popRoot
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Part 1
The key to getting the the particle system (or anything else) to only initialize once is understanding how and when SwiftUI recreates the View.
Right now your
GlobeViewControllerRepresentablecallsupdateUIViewControlleranytime anything in the@EnvironmentObject var viewModel: GlobeViewModelchanges.Because of this the
updateUIViewControllercan easily become complex because you have to create a series of checks to make sure you aren't duplicating work.But the alternative is to use SwiftUI's storage and identity management in your favor...
First, your ViewModel (or whatever anyone else prefers to call it) should be initialized as a
@StateObject. This is very important.Second, the
UIViewControllerRepresentableshould be as simple as possible (No SwiftUI property wrappers or additional arguments).These 2 things ensure that
makeUIViewControllerandupdateUIViewControlleronly get called once becauseGlobeViewControllerRepresentableidentity is tied to theStateObject.Once this is setup you can have a UIKit setup and a SwiftUI setup (best of both worlds).
Part 2
To call a function from the
UIViewControllerinSwiftUIyou can create a reference to theUIViewControllerin theViewModel.And to observe the changes in the ViewModel you can use
CombineYou can test this code with the code above and
You'll notice in the sample that
increaseRadiusdeclared in theUIViewControlleris called by theButtonvia the ViewModel and thesinkprintwhen the value changes.