Skip to content

Instantly share code, notes, and snippets.

@ariok
Created November 29, 2013 17:01
Show Gist options
  • Save ariok/7708767 to your computer and use it in GitHub Desktop.
Save ariok/7708767 to your computer and use it in GitHub Desktop.
Get Core Data entries between a date range
+ (NSArray*)allEntriesInContext:(NSManagedObjectContext*)context fromDate:(NSDate*)fromDate toDate:(NSDate*)toDate{
// Create the request
NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Entry"];
// Build the predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"date >= %@ && date <= %@ ", fromDate, toDate];
request.predicate = predicate;
// Define sorting
NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];
request.sortDescriptors = @[sortDesc];
// Execute the request
NSError *error;
NSArray *entries = [context executeFetchRequest:request error:&error];
if(error){
//!!!b Error management
}
return entries;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment