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

How to create a static instance of an Element as a default value when using Core Data?

Forums > SwiftUI

Hello,

When using Core Data, is there a way to create a static instance of an entity, for default values.

There are cases in which I feel the need to pass optional instances of an entity to a new view, which need to be unwrapped. This need arises especially when working with the new NavigationSplitView, because I need to pass an instance of an element to the detail view.

In NavigationSplitView, for navigation to the detail view, as far as I understand, one needs to create a @State variable for the selected instance of an object, and the type of this @State variable is an optional that needs unwrapping. I'd like to make the @State variable to be the type of the core data element.

In Content View:

@State selectedPersonEntity: PersonEntity?

Note: Where Person is the Core Data entity.

If not declared optional, the App file will ask you pass a default instance of Person. This is what I want created as some sort of Static value.

When passing to the Detail View, I can pass selectedPerson to a variable of type Person.

Thus far, I've managed to work with passing the Attributes of Person, and unwrapping them individually. But for entities with lots of attributes, it is quite tiresome to pass a dozen of attributes to a new view.

Any help would be appreciated! Thanks!

2      

A CoreData entity is an ObservableObject. Therefore, you have to use @ObservedObject and not @State.

For creating a CoreData entity you have always to create it in a NSManagedObjectContext. While you could create a temporary object in CoreData I would strongly advise not to do this in this use case. Everytime the View is initiated a default object would be created and if you don't pay attention you could end up with a whole lot of default objects in your data. I don't think this is what you want.

You could try to create your default object in a child context of your managedObjectContext and replace you default object with your current object. I don't know if this is a reasonable way to go. I don't have experience with this.

2      

In HWS+ Cleaning up Core Data which explains and make an example property. Think there may be a trail period that you use to check it out

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

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