NEW: Learn to build the incredible iOS 15 Weather app today! >>

Core Data

Forums > SwiftUI

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 SwiftUI 2.0 XCode 12. there really are no changes in moving from XCode 11 to XCode 12 on this.

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 Fred, there will be two files:

  • Fred+CoreDataClass.swift, which has the definition of the Fred class as a subclass of NSManagedObject.

  • Fred+CoreDataProperties.swift, which has the definition of each of the attributes of Fred marked as @NSManaged public var of the appropriate type as defined in the model editor. many of these will be optional types, although if you want to "front" them with code-facing names that are not optional, you can do that by adding your own extension to the Fred class.

the second of these files is where you will find the definition of the fetchRequest() definition for the Fred class, plus any accessors for relationships generated by XCode.

if you let XCode generate the files for you, there can be some disadvantages that do not really bother me.

  • changes to the Core Data model can occasionally confuse XCode (do a Product --> Clean Build Folder with the Option key held down, quit XCode, restart and build).
  • it may be difficult to quickly locate these files for inspection at first. but just right-click on any Fred reference in your source code and choose "Jump to Definition" and you can see the Fred+CoreDataClass.swift file. or right-click on any reference to a property/attribute of a Fred object in your source code and choose "Jump to Definition" and you can see the Fred+CoreDataProperties.swift file.

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:

I dont understand the file Persitence.swift and what its used for.

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 ...

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.

the Item entity is defined in the .xcdatamodeld file. it has only one attribute named timestamp which is a Date. and the ContentView() displays a list of the Items, showing the date for each one. that provides some sample data to see how things work.

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

1      

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

container = NSPersistentCloudKitContainer(name: "TestCDCKIOS14") 

to

container = NSPersistentCloudKitContainer(name: "Shared")

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Learn More

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.