|
I have a Core Data container with two entities, a Category and an Item. The Item can have one Category assigned and the Category can be assigned to many Items. What I need to do is group the items by category in a list in SwiftUI. The code below doesn't group all items by category, it only shows one item by category. How can I group all items that have the same category assigned under the same category group? Core Data Entities
Swiftui
Service Item Service Model
This is what I see, as you can see, only one |
|
Hi @fsdolphin,
I'm not sure how you have your
|
|
@Hectorcrdna - I have not been able to get it right but I think I'm now closer with your suggestion. If I undrstand you correctly you're suggesting to start iterating the Categories and then drilldown to the items from an other I just need to figure out how am I going to get the categories in SwiftUI since I'm using separate ServiceModels, one for Items and one for Categories, but I feel like I'm closer. Thanks a lot. |
|
@fsdolphin Correct, instead of using the |
|
It worked. I used the CategoryServiceModel in the first ForEach This is what worked for me. It basically returns and displays all categories and its items from the specified list. CategoryServiceModel
SwiftUI
|
|
Hello, It seems like you're facing an issue with grouping items by category in SwiftUI when using Core Data. To ensure that all items with the same category are grouped together, you can use the .groupBy modifier along with .fetchRequest to fetch the data correctly. Here's a sample code snippet to achieve this: import SwiftUI import CoreData struct ContentView: View { @Environment(.managedObjectContext) private var viewContext
} |
|
@instapro112 - I tried what you suggested but that only returns the categories, I actually need all the categories and items within the specified list. Thank you very much for your help. |
|
Oops -- you just reinforced the bot! I suspect they are learning which answers are most convincing. The giveaway is the generality of the advice. |
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!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.