UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.