简体   繁体   中英

Core Data NSPredicate to filter results

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM