views:

329

answers:

1

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.

A: 

Ohh, this is pretty easy, you just need to set the predicate of bid >= 43.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"bid >= %@", [NSNumber numberWithInt:43]];

I guess maybe you are having problem with the NSNumber, because Core Data saves many types under the NSNumber, includes: int, BOOL, etc... See here

sfa
Ok, in the data set I gave you, that just so happens to be the case. But that won't work for a larger set of data. Its a parent-child relationship. PID refers to another BID. So BID of 41 has 2 children, BID=42 and BID=47. Of that relationship, I only want the BID=47 returned. If the PID is 0, then add that to the subset as well, as long as there are no rows w/ a PID = BID.
Bryan
I think you should spend some more times to describe exactly what you want to do by editing the question clearly and if possible, give us some rules, otherwise, the answer would be useless.
sfa
you may want to publish your data models somewhere as well.
sfa