0
votes

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.

1
oops, even after you modified the question, it does not seem to be clear what you want to get from the data. Try to define a rule to fetch your data and you will see how to do it. With your explaination here, I don't see any rule to fetch the data you want. You said you want to get bid=44, but later on, you said 41? - Hoang Pham

1 Answers

0
votes

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