tags:

views:

18

answers:

1

Hi all,

I am new to Core Data and am trying to figure out how to set up a validation. I am tracking apartment buildings, where each Building entity has one or more Apartment entities. Each Apartment has a unitNumber property, which is unique within the scope of a Building.

There could be multiple Unit records with an identical value for the unitNumber property (i.e., "100A"), but there can not be multiple Unit records with that same unitNumber within the same Building.

So, the question is: how can I validate the uniqueness of an Apartment's unitNumber property within the scope of a Building?

Thanks, Bjorn

A: 

I would add a class method on my Apartment custom class which searches for an apartment using the predicate format

@"unitNumber==%d && building==%@",unitNumber,buildingInstance

If the result is nil, create a new apartment, or return the existing one.

Barry Wark
It seems to me that the validation really belongs in the Unit class.
Ben Gleeson
The Apartment object cannot tell it is unique without having knowledge of all other units. Since the Building class already has that knowledge it makes more sense to have the test in the Building class.
TechZen