Hi all,
Following along with the tutorial, I did not manage to get the result shown in the video. The view shows separate sections for all the candies.
To make the code work (even without the automatic sorting), I needed to follow the example of @rastazion23 and add a Persistence.swift file with code:
import CoreData
struct PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentContainer
init() {
container = NSPersistentContainer(name: "CoreDataProject")
container.loadPersistentStores{ (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolve Error: \(error)")
}
}
}
}
(source: https://www.hackingwithswift.com/forums/100-days-of-swiftui/day-54-bookworm-an-nsmanagedobject-of-class-book-must-have-a-valid-nsentitydescription/5043 on this same Forum)
This includes adding reference to the CoreDataProjectApp.swift file:
import SwiftUI
@main
struct CoreDataProjectApp: App {
let persistenceContainer = PersistenceController.shared
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext, persistenceContainer.container.viewContext)
}
}
}
As well as using the tip highlighted at the end of the lesson, in adding .mergeByPropertyObjectTrump in DataController.swift:
import CoreData
import Foundation
class DataController: ObservableObject {
let container = NSPersistentContainer(name: "CoreDataProject")
init() {
container.loadPersistentStores { description, error in
if let error = error {
print("Core Data failed to load: \(error.localizedDescription)")
return
}
self.container.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump
}
}
}
That being said, I still do not get the automatically sorted result. Not posting the ContentView.swift code as it is identical to the one in the video / description. Does anybody else have the same issue, and would anybody be able to explain to me what I'm doing wrong here?