-1

Sorry if i confuse anyone but i am going to try my best to describe my problem.

I have an entity named Document and another entity named Customer the Document has an attribute named CustomerID and that gets assigned from the Customer attribute CustomerID, and it has a updatedDate attribute.

I want to get the 10 most recent documents but only the most recent per CustomerID.

Bassem
  • 119
  • 2
  • 7

1 Answers1

0

If i understood you right, you want to get 10 most recent Documents fron a specified customer? That's easy.

NSFetchRequest *request = [[NSFetchRequest alloc] init];
   request.entity = [NSEntityDescription entityForName:@"Document" inManagedObjectContext:managedObjectContext];//Specify the entity Name
   request.predicate = [NSPredicate predicateWithFormat:@"customerID = %@",customerID];//Specify the ID you need
   request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"updatedDate" ascending:YES]];//Sorted by most recent ones
   request.fetchLimit = 10;//You'll get ten latest Documents
   NSError *error = nil;

 latestDocuments =  [managedObjectContext executeFetchRequest:request error:&error];

EDIT

As we found out, that the task is to get ten most recent documents, but just one per customer, i really see no way to do this somehow efficient(i mean,without IN query,that will contain customerID's you've already fetched),but juat to add a new one-to-one elationship to your customer - latestDocument, and a inverse to it, and just make request to the documents,in which isLatestToCustomer is not nil.

Nikita Pestrov
  • 5,876
  • 4
  • 31
  • 66
  • sorry, not for a specified customer. get the 10 most recent documents but only the documents with a unique customerID. `code` CustomerID = 1, saved a document 2 days ago customerID = 1, saved a document a day ago customerID = 2, saved a document a day ago customerID = 3, saved a document today i want to get the recent activity of documents but only the most recent of the unique customers. so it will fetch customerID = 1, document saved a day ago customerID = 2, document saved a day ago customerID = 3, document saved today – Bassem Apr 12 '12 at 04:06
  • You mean,ten latest documents,but only one document per customer? – Nikita Pestrov Apr 12 '12 at 04:30
  • yep, that sounds a lot more simpler :) – Bassem Apr 12 '12 at 04:42