|
Hi, I have a CoreData entity with a single string attribute. I created a couple of useful views making lists and a class conforming to ObservableObject with a few @Published vars to pass some data around (including the core data entity object itself). I now find myself with a second CoreData entity with a single string attribute (whoa... deja vu). So I thought, hey, it would be really cool if I could reuse the useful views and maybe just pass some enum over to use to switch between the appropriate source of truth I want to use in my view. I'm a bit lost though on how to do that and I'm not sure what I'm looking for to find a solution. I know I have an @EnvironmentObject that is going to have to change to another class to pass some data around as they both refer to different entities. I know my @FetchRequest has to change to a different keyPath to point to the appropriate CoreData entity. And I know I need to put the FetchedResults<> into a var to loop through in my list with a ForEach but it needs to be of the appropriate CoreData entity. I have this feeling that the answer is staring me right in the face, but I'm just not seeing it and not able to come up with what I'm looking for to solve something like this (hard to search for something when you are not quite sure what). Can someone point me in the right direction? I suppose I could duplicate my views and just modify the duplicates to refer to the appropriate CoreData entity and my class for passing data around, but I know there has to be a better way. What am I missing? |
|
You could use a view model instead of So you would have, say, two Core Data entities— Not saying this is the best way to do it or even if it should be done (I would say no), but that's one way you could approach it. |
|
You may want to consider using an If you had two entities, say Set You would then do all the fetchRequests for items on
|
|
Thanks for the reply @roosterboy. Well, there is another option that I can see based on your response (bringing things into a common item). I could go with a single entity and add a second Int16 attribute and use 0 and 1 and then filter things based on that. Would that be better or worse? I mean, it seems like it would be far easier for the programming as I would just be dealing with a single entity... but it would also be adding a bunch of 0s and 1s to the database and... should that be done? |
|
@Greenamberred ... Wow! OK... mind blown! I'm going to have to play with this a bit (whether it is ultimately the best way forward or not)... I had no idea I could do this (not surprising as I'm really new to all of this and I know Paul didn't cover this in 100 Days Of SwiftUI)... but this looks really promising as a way to keep these essentially same things in two distinct lists so I can use the appropriate one based on the context I need in the existing views. Thanks. |
|
@Greenamberred I'm not quite understanding this. Continuing the example of Book and Magazine... So In my .xcdatamodeld Book and Magazine both have a name String attribute and also a relationship set up with other entities. I add a WrittenMaterial entity and make it an abstract entity. Do I give it any attributes or relationship? I go to Book and Magazine and set them up as having a parent entity of WrittenMaterial. I'm also a bit confused about what to do with setting this up:
And then when calling it, one of these two:
|
|
The errors in XCode are because the In the data model definition, the
|
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
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.