Reputation: 307
I am using Core Data to retrieve some Data. How can I translate something like this SQL Statement to a Core Data one?
SELECT bus, direction from BUSES where latitude > 58.00 and longitude < 34.00 group by bus, direction
In other words, I like to group by BUS and DIRECTION but having a WHERE CLAUSE.
I've tried using this code:
request.propertiesToFetch = [NSArray arrayWithObjects:@"bus", @"direction", nil];
request.returnsDistinctResults = YES;
request.resultType = NSDictionaryResultType;
The thing is that if I retrieve only bus and direction, I cannot set an NSPredicate with latitude and longitude like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(lat > 58) AND (lat < 34)"];
[fetchRequest setPredicate:predicate];
I guess Core Data is only retrieving bus and direction, thats why the NSPredicate is not working.
Upvotes: 1
Views: 688
Reputation: 21460
Core Data is not SQL. In fact, it doesn't even have to use SQLite for its persistent store.
I always recommend you think about the problem you are trying to solve in terms of the entities and relationships you have defined in your model (rather than thinking about how you might do it in SQL).
I would recommend fetching the objects you want using a normal fetch request (don't specify propertiesToFetch) with your predicate and then grouping them yourself.
If you are only interested in one attribute, you can use valueForKey:
on the array of full objects to quickly get an array of just those values. For example:
NSArray *busses = [objects valueForKey:@"bus"];
Upvotes: 1