|
In SwiftUI 2.0 will you still have to make NSManagedObject Subclasses for your CoreData entitites? I know Paul has shown several videos on doing this in SwiftUI, because of the optional data types you get in CoreData but I haven't seen anything on whether this will still be the case for SwiftUI 2.0. |
|
hi Mark, yes, you will still have to make NSManagedObject Subclasses for your CoreData entitites in unless there's a reason to do otherwise, i will let XCode generate the right files for me and not go through the Editor --> Generate NSManagedObject Subclass procedure (as i think Paul did in videos). i set CodeGen to Class Definition for each entity. for an entity named
the second of these files is where you will find the definition of the if you let XCode generate the files for you, there can be some disadvantages that do not really bother me.
of course, if you generate either of these files manually, you have to remember to do that when you make changes, and any edits you've made in those files can/will be lost. and it sure does seem to confuse the hell out of source code control in git if you don't do it just right. hope that helps, DMG |
|
Thank you DMG. i downloaded the latest beta 6 of Xcode and wanted to play around with SwiftUI 2, but I dont understand the file Persitence.swift and what its used for. Also I noticed that when I created an app it put in an entity called Item and some code in other files im not sure what to do with. I dont even know where to start with getting started. Can you explain what i need to do to get started using XCode 12 and SwiftUI 2 with CoreData? I've looked on the old internet and dont come up with anything to explain what I need to do. I was hoping Paul had a tutorial out there that explained some of how to get started with CoreData and SwiftUI 2/XCode 12. Anyways, any help would be appreciated. I think after your above anwser I won't create the classes manually, I'll let XCode do that. and then create an extension and get rid of the optionals so I wont have to deal with nil coalescing. |
|
hi Mark, some quick answers for you:
this is just the code that makes Core Data startup, connecting with where its data is stored. it was often called the "Core Data Stack," but it's now been reduced to just a few lines of code in recent iOS releases. you'll not need use anything directly from the PersistenceController. what you really need to know is that your ContentView has the Core Data already set up for you when it opens and provides the managedObjectContext in its environment. of note: the first releases of XCode 12 did not even have the Core Data piece attached right away. there have been plenty of home-spun solutions to attaching the Core Data component, which involved moving the Core Data code from the AppDelegate and SceneDelegate (in XCode 11) into the new XCode 12 App struct. i have done this myself in the ShoppingList project with which you (and some others) are already familiar. remember that this is still beta software. i expect Persistence.swift to change a little going forward. gosh, there might even be documentation soon ...
the you'll be rewriting the ContentView yourself, so you can rip out its body code and start over. (don't throw away the addItem() and deleteItems() functions just yet -- they could be a template you'll want to keep handy on how to add and remove items.) go to the .xcdatamodeld file, remove the Item entity (it does no harm if you leave it), and then add your own entities (which were something like Hymn and Playlist in this earlier conversation as well as this follow-up conversation). the process of working with the Core Data model is exactly the same as any you've seen around the internet: that hasn't changed for some time. what you saw in Paul's post on defining entities and attributes will be exactly the same (except for the CodeGen setting: Paul used a manual code generation process). just remember that your simulator may go a little crazy at times if you've stored data using an earlier model and then you change the model. just delete the app from the simulator and start over. hope that helps, DMG |
|
Thank you DMG. That helped me a lot as well. Also, for everyone's sake, when I start a project with Core Data and Cloudkit in beta 6 the Persistence (stack) is initializing the container with your app name but naming the CD Model as "Shared". Simple fix is to switch
to
|
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.