4

I have this statement that im trying to figure out how to use in core data.

SELECT 
(3963.0 *  acos(sin(".$StartLat."/57.2958) *   sin(latitude/57.2958) + cos(".$StartLat."/57.2958) *  cos(latitude/57.2958) * cos(longitude/57.2958 - ".$StartLon."/57.2958)))   as distance 
FROM locations
ORDER BY distance
LIMIT 0, 10

Im guessing this is the part i need to modify:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Title" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

Thanks in advance.

user648753
  • 167
  • 1
  • 9

1 Answers1

4

Simple answer: you can't.

Real answer: you haven't understood what Core Data is.

Core Data is not a database. It is not SQL. It is not a relational database in any way shape or form.

Core Data is a framework for abstracting the persistence of objects from their in-memory representation. It is an object management framework. You interact with it via objects. You do not create queries. You do not execute SQL. You request objects. Full blown, NSObject subclasses.

Please go read the Core Data Programming Guide.

(sorry if this comes off as harsh, but this is such a commonly mis-understood thing that it bears being totally explicit)

Community
  • 1
  • 1
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Thanks for clarifying. I ended up using sqlite for my project. – user648753 Aug 09 '11 at 20:42
  • 1
    This is such a strange comment. Core Data is often backed by SQLite, yes SQL. Analogously Hibernate is an ORM abstraction layer and if you imagine you can use either and just forget there is SQL under the hood, you will end up in trouble. Like this http://stackoverflow.com/questions/97197/what-is-the-n1-selects-problem – Martin Algesten Jun 13 '12 at 09:14
  • ORM in Dot net framework provides way to query the model using plain sql too.now iOS 9 has also been introduced ...is there any way that we can use plain sql against objects... anyhow if core-data background is sqlite ..it's right to expect core-data would allow right ! – Manju Oct 05 '15 at 18:47
  • This is a poor answer. Clearly the question is how to get the same results from CoreData as I get from using this sql select statement from SQLite. I too am searching for how to interpret sql queries in the coredata environment and the number of non answers spouting in bold letters that coredata is not a database is very annoying. Before coming off as “harsh” it would behove you to step into the shoes of the person asking the question rather than not answering it. – Rillieux Dec 15 '20 at 11:30
  • As I see this subject is still of interest: I first agree to Rillieux. Second, there are frameworks around (https://github.com/stepencelis/SQLite.swift ). But they are not "official" Apple frameworks. Third: Core Data is a different philosophy: It has no joins, nor keys like a relational database. So it is probably better to use Core Data features. But then, I am afraid you need to do things like distinct, group by, having ... on your own. – Christian Krueger Apr 26 '21 at 15:15