TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Moving relationship to many data from CoreData to an array of custom struct?

Forums > SwiftUI

Hi,

I'm having some trouble figuring this one out. I'm using CoreData. I have an entity named "Item". Item contains attributes for name and amount. I have another entity named "Transaction" that has a "to many" relationship to items.

I find myself needing to edit an array of Item, but not as CoreData. I created a struct that mimics the CoreData for this use named "WorkingItem".

If there are existing items in the Transaction, I need to put that into an @State var to pass into another view as a @Binding (I likely can do that .onAppear in my view). In that view I need to work with my array of WorkingItem. The problem I'm having is getting from the NSSet returned from CoreData to the array of WorkingItem. Anyone have any ideas?

I thought of adding an extension on Transaction with a computed property that could return the array of ItemizeItem, but I'm not really sure how to do the conversion itself.

2      

You can turn an NSSet into a Swift array like this:

let nssetAsArray = nsset.allObjects

But you know you don't have to use NSSet with Core Data, right? You can just use a Swift Set. So instead of NSSet, which type erases the items in it, you would make the type of the managed property something like Set<MyType>.

And you can turn a Swift Set into an array like so:

let swiftSetAsArray = Array(swiftSet)

3      

Thanks @roosterboy.

I'm not entirely sure I'm following this or maybe explaining it the best. My database is returning the "items" (to-many) relationship as a NSSet when look up a particular "Transaction". There is a situation though where the "items" might be empty and the "Transaction" has not been saved yet (I could be creating a new "Transaction"). So I can't directly create a new "Item" (or multiple new items) in the view context in this case because there is no record saved yet to attach it to. This is why I'm trying to come up with an intermediate way to work with the existing or new data and hit upon the idea of just mirroring the stored data "Item" with my "WorkingItem" struct.

So my thought process is that the editor view for "Transaction" opens and has an @State var that will hold an array of my "WorkingItem" struct and it starts off as empty. If the editor is opening an existing saved "Transaction" and checks items .onAppear it may (or may not) find any. If there are some, I need a way to get them into my @State var array of "WorkingItem". So like: Item with attributes for name "Vision Pro" and amount (as decimal) 3499 becomes -> WorkingItem(name: "Vision Pro", amount: 3499). When the user opens the item editor view they will be able to add or remove WorkingItem to/from the array which is brought in as a @Binding. When the item editor view is closed my @State var array is up-to-date with the changes (if any) that were made. When the "Transaction" is finally saved it creates/updates it in the view context and also deletes existing items in the relationship first (if there were any) and generates new ones from the WorkingItem array to attach to the new/updated parent Transaction. If the Transaction is canceled, then the WorkingItems array is gone with it and if the Transaction was existing any items attached to it remain as they were.

The first thing that is confusing me is that, yes, I can turn the NSSet returned to a Swift array - but that will only return an array of Item which I can't really work directly with in this particular case.

No I didn't know that I don't have to use NSSet with Core Data… I thought that was the only option a "to-many" relationship would have, but I'm not sure how that helps me in this case. I did consider a transformable type to set up the items as an array within the Transaction itself, but I have to admit as I looked into that my brain sort of melted at what was involved and the "to-many" relationship sounded like a better (or at least easier-to-understand and implement) way to go. But if that is indeed what you are suggesting, then maybe I just need to take the time and try to make sense of it (and I think Mark Moeykens new Core Data Mastery in SwiftUI book - which I didn't have at the time I had looked into this) has a section dedicated to this (although I don't recall an array example being included). Perhaps worth a revisit?

Or is there just something in your answer that I'm just not getting (definitely a possibility)?

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.