0

I have a Event Entity with a action as Attribute. The action is a NSString that gets saved to core data on each event in my iOS app. The event.action will always be a predefined string e.g.. "Work" "Home" "Lunch"

How can I use NSPredicate to retrieve the last 5 actions and give me the action that was performed the most?

Are there any good tutorials on using NSPredicate?

Johan de Klerk
  • 2,515
  • 3
  • 25
  • 35

1 Answers1

1

Could you please try the following, hope at lease guide you :)

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event"
        inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", @[@"Work", @"Home", @"Lunch"]]; //contain your actions

[request setPredicate:predicate];
request.fetchLimit = 5;// retrieve the last 5 actions
NSError *error;
NSArray *yourActions = [managedObjectContext executeFetchRequest:request error:&error];

for (Event *event in yourActions)
{
  // find the action that was perform the most in last 5 actions
}

please give me a feedback, so I know what's happen, so I can edit the code to help you :).

thanks for the following references: Using Core Data with NSPredicate and NSDate Dynamically setting fetchLimit for NSFetchedResultsController http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html#//apple_ref/doc/uid/TP40001794-CJBDBHCB

Community
  • 1
  • 1
piam
  • 653
  • 6
  • 13