Reputation: 2242
I am using the new SwiftUI App protocol and passing my Core Data NSPersistentContainer via @Environment:
final class PersistentStore: ObservableObject {
static let shared = PersistentStore()
let persistentContainer: NSPersistentContainer
private init() {
self.persistentContainer = {
let container = NSPersistentContainer(name: "SillyModel")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
}
func saveContext() {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}
@main
struct SillyApp: App {
@Environment(\.scenePhase) private var scenePhase
var body: some Scene {
WindowGroup {
NavigationView {
ProjectGridView()
}
.environment(\.managedObjectContext, PersistentStore.shared.persistentContainer.viewContext)
}
}
}
I then use @Enviroment in ProjectGridView to get the managedObjectContect from the environment, attempt a @FetchRequest and display a new LazyVGrid:
struct ProjectGridView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(
entity: Project.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \Project.title, ascending: true)
]
) var projects: FetchedResults<Project>
let columns = [GridItem(.adaptive(minimum: 80.0))]
var body: some View {
return ScrollView {
LazyVGrid(columns: columns, spacing: 20) {
ForEach(projects, id:\.title) { (project: Project) in
Text("\(project.title ?? "")")
}
}
.padding(.horizontal)
}
}
}
I get this error:
[error] error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'Project' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?
I've tried writing code that saves new Project()s and even have @FetchRequest working elsewhere. I'm guessing it may have something to do with the "Lazy" part of LazyVGrid? Any ideas?
Upvotes: 1
Views: 1379
Reputation: 2242
Okay, found a fix from the Apple Developer forum. This appears to simply be a beta bug:
To fix, create an extension to your model class:
import Foundation
import CoreData
extension Project {
class func swiftUIFetchRequest() -> NSFetchRequest<Project> {
let request = NSFetchRequest<Project>(entityName: "Project")
request.sortDescriptors = [NSSortDescriptor(keyPath: \Project.title, ascending: true)] /* You can add custom sort descriptors here */
return request
}
}
Then call @FetchRequest like this:
@FetchRequest(fetchRequest: Project.swiftUIFetchRequest()) var projects: FetchedResults<Project>
Thank you to the person in the Apple Developer forum who figured this out.
Upvotes: 2