|
Pretty new to SwiftUI and was wondering if anyone knows if Core Data support for the Lycycle Choice of SwiftUI App vs App Kit Delegate is going to be added, or if we can "Simply" copy and past the App Delegate portion that supports Core Data into an app based on SwiftUI App template. |
|
|
|
Just create your own CoreData class and inject it into your app environment. Below example is for PersistentCloudKitContainer but can easily be changes to just a PersistentContainer.
And the custom class
|
|
|
|
you can put the class wherever you want. i did put it in a separate framework target since i wanted my core data model be shared between ios, macos and tvos. |
|
When following this, i keep getting the error, cannot find type "MyEntity" in scope. My App.swift and the CoreData.swift files below (created the coredata.swift to hold the public class)
This is the start of my content view:
Any ideas what is causing this from a code perspective? Edit: I have copied this post over to apple dev also, not sure where it is best placed. |
|
I think the error is in the last part of
FetchRequest<MyHumidors> should be FetchedResults<MyHumidors> |
|
Is it a bad thing that I want the fetch behavior for Core Data to go through a View Model and the SWIFTUI not to care about the data source or not? |
|
I am new to SwiftUI and since I started using Xcode 12 beta, I was up against the wall trying to use Core Data. I had no problem when I was using Xcode 11 with the AppDelegate and SceneDelegate files. I was actually starting to wrap my head around it. What I discovered was after checking the coreData box on startup, Xcode pre-populated my ContentView() with a whole series of content pertaining to the "timestamp" attribute which it gives by default after it created an Entity named "Item". That was not the name of my original Entity, but I decided to leave it as "Item" for testing sake. I decided to delete all the content in the body view and leave what xcode provided in the properties area as seen below. After I left the "timestamp" attribute intact and added my new attributes, I only had to change the keyPath to "Item.name" instead of "Item.timestamp" which xcode originally gave me. import SwiftUI import CoreData struct ContentView: View {
This did the trick and my App is saving perfectly as before. Another thing is that "viewContext" is used more often than what I used to use "managedObjectContext" for, so pay attention to that. Now, since I'm new to this, I'm hoping this might help someone that might have been struggling with this issue. |
SAVE 50% All our books and bundles are half price for Black Friday, 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.
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.
Link copied to your pasteboard.