Code was working in Xcode 11.3 but after updating to 11.4, I am now getting two errors:
Context in environment is not connected to a persistent store coordinator.
Thread 1: Fatal error: No ObservableObject of type UpdateTagView found. A View.environmentObject(_:) for UpdateTagView may be missing as an ancestor of this view.
This is how I add the Objects to the Environment:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
context.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
let em = EventManager()
let tagger = UpdateTagView()
let contentView = ContentView()
.environment(\.managedObjectContext, context)
.environmentObject(em)
.environmentObject(tagger)
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
I call them inside the View with @EnvironmentObject:
struct ViewName: View {
@EnvironmentObject var em: EventManager
@EnvironmentObject var tagger: UpdateTagView
Here is an example of what is in the ObservableObject:
class UpdateTagView: ObservableObject {
@Published var counter: Int = 0
@Published var language: String = "English"
}
Cheers~