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 Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.