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

How to complete tasks requiring SceneDelegate with Xcode 12 (Days 57 and 58)

Forums > 100 Days of SwiftUI

Given that SceneDelegate has been removed with Xcode 12, I'm wondering how I can follow the instructions from this video to introduce constraints.

Specifically, where should I write this code now:

context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy

Have extensively googled for solutions, but seem to come short, so would appreciate any suggestions!

2      

Did you check the "Core Data" checkbox when creating the project? This is property on the Core Data viewContext.

2      

@nemecek-filip yep, I did!

As far as I can get from Paul's video, previously in SceneDelegate.swift you'd have this code:

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy

And with Xcode 12, in Persistence.swift, I have this:

static var preview: PersistenceController = {
        let result = PersistenceController(inMemory: true)
        let viewContext = result.container.viewContext
        ...

Even if I add the viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy to the bottom of it, that doesn't seem to do the trick.

So wondering if Persistence.swift is even the right place to add this, or it should be done inside AppNameApp.swift file.

2      

Can you post the entire Persistence.swift file? Maybe in your code you are setting the property only for previews?

2      

@nemecek-filip Sure thing:

import CoreData

struct PersistenceController {
    static let shared = PersistenceController()

    static var preview: PersistenceController = {
        let result = PersistenceController(inMemory: true)
        let viewContext = result.container.viewContext
        viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        for _ in 0..<10 {
            let newItem = Movie(context: viewContext)
            newItem.title = ""
            newItem.director = ""
            newItem.year = 2016
        }
        do {
            try viewContext.save()
        } catch {
            // Replace this implementation with code to handle the error appropriately.
            // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            let nsError = error as NSError
            fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
        }
        return result
    }()

    let container: NSPersistentContainer

    init(inMemory: Bool = false) {
        container = NSPersistentContainer(name: "CoreDataProject")
        if inMemory {
            container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
        }
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

                /*
                Typical reasons for an error here include:
                * The parent directory does not exist, cannot be created, or disallows writing.
                * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                * The device is out of space.
                * The store could not be migrated to the current model version.
                Check the error message to determine what the actual problem was.
                */
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
    }
}

2      

I would modify the init and added at the end something like this:

container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
container.viewContext.automaticallyMergesChangesFromParent = true

2      

SceneDelegate is only available when you create an app with UIKit App delegate lifecycle. Things change when you use SwiftUI lifecycle. So the next best thing is the main App file.

2      

@nemecek-filip

Amazing, it worked! Thanks so much!

2      

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.