I have a SwiftUI View struct to which I pass a Place object in the init function. Using an id I want to load a corresponding UserPlaceInfo from CoreData. Unfortunately my app crashes when I try to access the fetchedResults in the init function. If I use the same code in onAppear this works without problems.
private var fetchRequest : FetchRequest <UserPlaceInfo>
private var fetchedResults: FetchedResults<UserPlaceInfo> { fetchRequest.wrappedValue }
init(place : Place) {
self.place = place
fetchRequest = FetchRequest<UserPlaceInfo>(entity : UserPlaceInfo.entity(), sortDescriptors : [] , predicate: NSPredicate(format: "id == %@", place.id))
// Application crashes here!
let count = fetchedResults.count
}
Why is that and what can I do about it? Am I doing something wrong or are the fetchedResults not yet available at this time? I need the result very early, because if there is no UserPlaceInfo in CoreData, I have to create such an object.
UPDATE
Adding fetchRequest.update() helped a bit. Now I am getting a more useful error message: Context in environment is not connected to a persistent store coordinator
I still don't understand what is going on. I add the managedObjectContext to the environment in the SceneDelegate and the fetch works fine if I do it in onAppear.
fetchRequest.update()before fetchedResults usage - Asperilazyforvar persistentContainerin the AppDelegate or making sure it gets initialised before the ContentView is created is a solution. - Joakim Danielson