tags:

views:

70

answers:

1

Welcome

i use Core Data to store datas. i need such a method which returns only the last 7 elements of entity. my question is how should i modify this code ( it fetchs all of elements, but i need only last 7)

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Trip" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[ NSFetchRequest alloc] init];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:NO]; 
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];  
[request setSortDescriptors:sortDescriptors];  
[sortDescriptor release];  
NSError *error;  
tripArray = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
A: 

Define last? Core Data does not have a concept of order internally. If you mean by the farthest away based on your distance property then you can do the following:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Trip" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[ NSFetchRequest alloc] init];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:NO]; 
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];  
[request setSortDescriptors:sortDescriptors];  
[request setFetchLimit:7];
[sortDescriptor release];  
NSError *error;  
NSArray *tripArray = [managedObjectContext executeFetchRequest:request error:&error];

Note that the addition of the -setFetchLimit: will cause this request to only return 7 results. It will return the "first" 7 based on your sort. So if you want the closest, reverse the ascending: portion of your sort.

-mutableCopy

There is absolutely no point in calling -mutableCopy on the NSArray that is returned from -executeFetchRequest: error:. Adding objects to that NSArray will not add them to Core Data and removing them from that NSArray will not remove them from Core Data. Therefore it has absolutely no value and is just wasteful.

Do you remember where you saw that? I have been trying to track it down for a while now.

Marcus S. Zarra
hmm thank you very much. and what if you do not have 7 items yet? it thrown 'NSInternalInconsistencyException', reason: "FATAL ERROR: The persistent cache of section information does not match the current configuration. You have illegally mutated the NSFetchedResultsController's fetch request, its predicate, or its sort descriptor without either disabling caching or using +deleteCacheWithName:"I do not remember exactly, but certainly some kind of tutorial. i'm totally beginner in CoreData. all my knowledge is from google and forums, some sample from apple dev center, and of course API doc.
Victor
Sounds like you are hard-coding 7 there somewhere when you should be looking at how many items are returned.
Marcus S. Zarra
Could you recommend me some book or any useful source to High-Level CoreData programming please?
Victor
The Apple documentation is decent and of course I obviously recommend my own book published by [The Pragmatic Programmer](http://pragprog.com/titles/mzcd/core-data). I believe there is also a book coming out from Addison Wesley. While I have not read it, I do know the authors and expect it to be quite good. Other resources you should consider are [CIMGF](http://www.cimgf.com), [Mac Developer Network](http://www.mac-developer-network.com/) and [Cocoa With Love](http://cocoawithlove.com/). Those are more generic resources but have a few Core Data articles.
Marcus S. Zarra
The biggest hurtle to get over when learning Core Data is not to think of it as a wrapper around SQL. Core Data is not based on SQL, it does not always use SQL and when it does, the SQL elements are deeply hidden. Core Data is an object graph persistent framework that shares very little in common with relational databases.
TechZen