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

Count Related Items

Forums > Articles

I'm using SwiftData and I have two models: Category and Item.

I want to have a view that lists the categories and displays a count of the number of items related to that category.

I read through counting results without fetching (https://www.hackingwithswift.com/quick-start/swiftdata/how-to-count-results-without-loading-them).

But I am still really new to coding and don't know how to write a predicate that is specific to each category (model A) and yet gets a count from a different model (items related to each specific category).

I'm also struggling because most of the tutorials and other resource information predates SwiftData.

I sort of assume I'll have to write the specifics outside the query and then call them in the predicate but I don't even know where to start. Does anyone have any suggestions?

3      

Hacking with Swift is sponsored by RevenueCat.

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.

Learn more here

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.