WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Day 58: cannot get the results sorted into sections automatically

Forums > 100 Days of SwiftUI

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?

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.