Core Data NSPredicate to filter results

Posted by Bryan on Stack Overflow See other posts from Stack Overflow or by Bryan
Published on 2010-03-16T16:23:16Z Indexed on 2010/03/16 18:41 UTC
Read the original article Hit count: 432

I have a NSManagedObject that contains a bID and a pID. Within the set of NSManagedObjects, I only want a subset returned and I'm struggling to find the correct NSPredicate or way to get what I need out of Core Data.

Here's my full list:

bid pid
41 0
42 41
43 0
44 0
47 41
48 0 49 0 50 43

There is a parent-child relationship above.

Rules:

If a record's PID = 0, it means that that record IS a parent record. If a record's PID != 0, then that record's PID refers to it's parent record's BID.

Example:

1) BID = 41 is a parent record. Why? Because records BID=42 and record BID=47 have PID's of 41, meaning those are children of its PID record.
2) BID = 42 has a parent record with a BID = 41.
3) BID = 43 is a parent record.
4) BID = 44 is a parent record.
5) BID = 47 has a parent record with a BID = 41 because its PID = 41. See #1 above.
6) BID = 48 is a parent record.
7) BID = 49 is a parent record.
8) BID = 50 is a child record, and its parent record has a BID = 43.

See the pattern?

Now, basically from that, I want only the following rows fetched:

bid pid
44 0
47 41
48 0 49 0 50 43

BID = 41, BID = 48, BID = 49 should all be returned because there are no records with a PID equal to their BID.

BID = 47 should be returned because it is the most recent child of PID = 41.
BID = 50 should be returned because it is the most recent child of PID = 43.

Hope this helps explain it more.

© Stack Overflow or respective owner

Related posts about core-data

Related posts about iphone