Core Data and SwiftUI in Xcode 12

When creating an IOS project and using the new (non UIKit) SwiftUI Template, it seems that you cannot get Core Data support.

So is not possible at all to have Core Data in an app using this new style of SwfitUI API?

If not, how would one include Core Data manually?

Replies

It is possible to use Core Data with SwiftUI. You can already check the checkbox while creating the project.

Maybe this WWDC video helps: https://developer.apple.com/videos/play/wwdc2019/230/
or the tutorial on: raywenderlich.com/9335365-core-data-with-swiftui-tutorial-getting-started
You need to create a custom Core Data class and inject it into ContentView()

I don‘t have a default file at hand but you can inject it like below

Code Block
let context = PersistentCloudKitContainer.persistentContainer.viewContext
ContentView().environment(\.managedObjectContext, context)




Below my CloudKitContainer but you can simply change that to a regular PersistentContainer class

Code Block
import CoreData
public class PersistentCloudKitContainer {
// MARK: - Define Constants / Variables
public static var context: NSManagedObjectContext {
return persistentContainer.viewContext
}
// MARK: - Initializer
private init() {}
// MARK: - Core Data stack
public static var persistentContainer: NSPersistentCloudKitContainer = {
let container = NSPersistentCloudKitContainer(name: "Container_Name")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
// MARK: - Core Data Saving support
public static func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}


I was struggling with this also. It doesn't recognize my entities.
It looks like a bug. I noticed that too, but only the first time you create a project. To fix it, change the popup from SwiftUI to Storyboard, the Core Data checkbox will then be enabled. You can then check the Core Data checkbox, and make sure to change the Interface back to SwiftUI.
Actually I don't think it's a bug. If the Life Cycle is set to SwiftUI App, the Core Data checkbox is disabled. I'm assuming either it's not ready yet or it won't be enabled for some specific reason.
Xcode 12 does not currently include a project template that demonstrates how to use Core Data with the SwiftUI Life Cycle option. You can, of course, set up a Core Data persistence stack by hand and use that in your application, including with the SwiftUI Life Cycle; all the non-SwiftUI Life Cycle project template does is add code and a data model to your project.
I have encountered the same issue. Changing to storyboard will not help. When you set back to swift UI the checkboxes disappear. It is annoying. I need Core Data for my apps. Apple has shown so many "amazing" things and nobody even mentioned, that lacking Core Data in Swift UI would be a great drawback.

Making apps like "which books do I read now" makes not much sense. I need much more data than 7 book titles.

I hope they will change it.
Just create your own Core Data class and attach it to your app.


Code Block
import SwiftUI
import CoreData
@main
struct TestApp: App {
let context = PersistentCloudKitContainer.persistentContainer.viewContext
var body: some Scene {
WindowGroup {
ContentView().environment(\.managedObjectContext, context)
}
}
}



And the custom class

Code Block
import CoreData
public class PersistentCloudKitContainer {
// MARK: - Define Constants / Variables
public static var context: NSManagedObjectContext {
return persistentContainer.viewContext
}
// MARK: - Initializer
private init() {}
// MARK: - Core Data stack
public static var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "Model")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
return container
}()
// MARK: - Core Data Saving support
public static func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}