Marco Almeida
Marco Almeida

Reputation: 1303

How to count Attributes from an Entity in Core Data with Swift and iOS 9?

I am stuck with Core Data on counting the amount of item in a particular entity item.

Here´s how it is going:

I have 2 entities: Categories and Subcategories. The relationship is To-Many from Categories to Subcategories.

On each of them I have the name attribute.

So what I need is to get the amount of Subcategories I have by name and store that amount on an attribute I will create on Categories under the subcategoriesAmount attribute.

The main view is a tableview with Categories I can add and also have on each cell a label that will display the amount of subcategories, and clicking on each cell, after a category is added, with load another tableview with the Subcategories that I can add or were already added. So as I keep adding subcategories and later on get back to the Categories table, I wan to display the amount of subcategories corresponding to the category it is connected.

So how can I do that?

Upvotes: 0

Views: 647

Answers (1)

Josh Paradroid
Josh Paradroid

Reputation: 1414

Assuming your relationships are setup correctly, you should just be able to access the subcategories of your category directly. For example: category.subcategories.count.

If you wish to store that as an attribute keep in mind that you'll need to update this value every time you add or remove a subcategory. It sounds a bit like a premature optimisation to me.

Upvotes: 1

Related Questions