The @Environment(.undoManager) is nil when the app cold starts and remains nil until the user change views. When the view is changed, undoManager becomes set and undo functionality is available.
In the below gif you can see how undo is not available until the user switches to a new view "Focus" and then back.
Here's the SwiftUI code where the undoManager environment is set:
@Environment(\.managedObjectContext) private var viewContext
@Environment(\.undoManager) private var undoManager
[...]
listOfCards.task{
viewContext.undoManager = undoManager //nil when starting from cold start
}
The @main App instantiates a NSPersistentCloudKitContainer and it sets it as the managedObjectContext
mainView
.environment(\.managedObjectContext, persistenceController.container.viewContext)
Here's some of the things I've tried so far to solve this:
A) Adding in each view the following...
.onChange(of: undoManager) { _ in
print(undoManager)
viewContext.undoManager = undoManager}
to detect when the undoManager is set and link it to the viewContext. No luck, does not get called.
B) Also have tried adding an observer:
private let undoObserver = NotificationCenter.default.publisher(for: .NSUndoManagerCheckpoint)
.onReceive(undoObserver, perform: { _ in
print(undoManager)
viewContext.undoManager = undoManager
})
Also without luck, the only thing it works is going to another view and back, then UndoManager is set correctly. Any ideas? Am I missing something obvious?