I have a SwiftUI View that presents and updates data originating in an @ObservedObject. The code sample below works as expected UNLESS the @ObservedObject happens to be a subclass of UIViewController. In this case, data updates are made to the @ObservedObject but they do not trigger a reload of the View content as expected.
Here is the SwiftUI View:
struct ContentView : View {
@ObservedObject var scene: SceneViewController
var body: some View {
VStack {
Text("The time is: \(scene.currentSimTimestamp.description(with: .current))")
Button(action: {self.scene.currentSimTimestamp = Date()},
label: {Text("Update")})
}
}
}
And here is the @ObservedObject:
class SceneViewController: UIViewController, ObservableObject {
@Published var currentSimTimestamp: Date = Date()
}
Pressing the "Update" button will cause the value stored in scene.currentSimTimestamp to update, HOWEVER ContentView will not reload (the screen won't update to reflect the data change).
Changing class SceneViewController: UIViewController, ObservableObject {
to class SceneViewController: ObservableObject {
will cause the update to display as expected.
It seems this may be a bug, as the Apple documentation and videos that I have seen seem to suggest that any Class can adopt ObservableObject, and indeed there is no compiler issue generated. But am I missing something?
(Added sample SceneDelegate code to reproduce the sample code into a project, below)
import UIKit
import SwiftUI
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
let sceneController: SceneViewController = SceneViewController()
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView(scene: sceneController)
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
...