user1241117
user1241117

Reputation: 1

Setting predicate based on multiple entities using core data

I have two tables oldaddress and newaddress with common field personID. I have to get details of personID from newaddress which exists in oldaddress also. How to set entity and predicate for the same in core data?

equivalent query is

select a.personID  from new address a, oldaddress b where a.personID=b.personID

Upvotes: 0

Views: 736

Answers (1)

Mundi
Mundi

Reputation: 80273

You have not grasped the essence of Core Data. As pointed out by Martin R, it is an object graph, not a database.

What you describe is a join table operation, something that is modelled in Core Data via so-called relationships, eliminating the need for foreign keys such as personID.

Also, to create different identities that represent the same kind of object is also bad practice.

Thus, your model should look something like this:

Address <<------>> User

and Address should have a boolean property like isOld.

Your predicate becomes extremely simple when fetching Users.

[NSPredicate predicateWithFormat:@"addresses.@count = 2"]; 

Alternatively, you can have two one-to-many relationships from User to Address if you prefer.

User.oldAddress <<-----------> Address
User.newAddress <<-----------> Address

with a predicate like this

[NSPredicate predicateWithFormat:@"oldAddress != NULL && newAddress != NULL"]

Upvotes: 3

Related Questions