I'm supporting iOS 15, my code is working properly at runtime, but my previews are crashing. I understand that maybe the issue is about providing them a managed context , but really do not understand how to do it. I found someone found solutions, but not working for me. I'm not using AppDelegate. regarding error, sometime I get "an nsmanagedobject of class lesson must have a valid nsentitydescription" sometimes the preview simply crashes with no message and a black screen with a red "x"
import SwiftUI
@main
struct CoreDataIOS15FilesAddedApp: App {
@StateObject private var dataController = DataController()
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext, dataController.container.viewContext)
}
}
}
import CoreData
import Foundation
class DataController: ObservableObject {
let container = NSPersistentContainer(name: "Bookshelf")
init() {
container.loadPersistentStores { description, error in
if let error {
print("error: \(error.localizedDescription)")
} else {
print("✅ loaded persistent stores")
}
}
}
}
import SwiftUI
struct ContentView: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest (sortDescriptors: [], predicate: nil) var lessons: FetchedResults<Lesson>
@State var showAddView = false
var body: some View {
NavigationView {
VStack {
List {
ForEach(lessons, id: \.self) { lesson in
NavigationLink {
DetailView(lesson: lesson)
} label: {
HStack {
Text( "\(lesson.mainAssertion ?? "ND")")
.font(.body)
}
}
}
}
.sheet(isPresented: $showAddView) {
AddDataView()
}
//************************************************************
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button {
showAddView = true
} label: {
Label("Add Lesson", systemImage: "plus")
}
}
}
}
.padding()
}
}
}
//#Preview {
// ContentView() //crashes when I save
//}
import SwiftUI
struct AddDataView: View {
@Environment(\.managedObjectContext) var moc
@Environment(\.dismiss) var dismiss
@State private var mainAssertion: String = ""
@State private var origin: String = ""
var body: some View {
NavigationView {
VStack {
Form {
TextField("MainAssertion", text: $mainAssertion)
TextField("Origin", text: $origin)
}
Button("Add") {
let newLesson = Lesson(context: moc)
newLesson.mainAssertion = mainAssertion
newLesson.origin = origin
do {
try moc.save()
dismiss()
} catch {
print(error.localizedDescription)
}
}
}
.navigationBarTitle("Add Data", displayMode: .inline)
.navigationBarItems(trailing: Button("Cancel") {
dismiss()
})
}
}
}
//#Preview {
// AddDataView() //crashes when I save
//}
import SwiftUI
struct DetailView: View {
let lesson: Lesson
@Environment(\.managedObjectContext) var moc
@Environment(\.dismiss) var dismiss
@State private var showingDeleteAlert = false
@State private var genericAlert = false
@State private var alertTitle = ""
@State private var alertMessage = ""
var body: some View {
Form {
Section {
DeatailRowView(name: "MainAssertion", info: "\(lesson.mainAssertion ?? "ND" )")
DeatailRowView(name: "Origin", info: "\(lesson.origin ?? "ND")")
}
}
.navigationTitle("Detail View")
}
}
//struct DetailView_Previews: PreviewProvider {
// static var previews: some View {
// DetailView(lesson: Lesson())
// }
//}