How to use NSPredicate for fetching property having NSDate type?

Go To


I have a column in core data that stores a date (eg:2006-01-26). I used an array to store all the years by using NSDateComponents on these stored dates.(e.g.: on above date if i use NSDateComponents, I get 2006 as a year). These years are NSNumbers, and using them in my predicate I want to fetch the column which is NSDate with all Date components. And fetched records should be only of particular year. I am now confused about how I could match a year components with one of those in array, and create predicate.

- (void)viewDidLoad
    [super viewDidLoad];

    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    NSError *error;

    //AppDelegate *app = [[UIApplication sharedApplication]delegate];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Movies" inManagedObjectContext:self.managedObjectContxt];
    [request setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] 
                                        initWithKey:@"releaseDate" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];
    [request setSortDescriptors:sortDescriptors];
    //[request setReturnsDistinctResults:YES ];
   // [request setEntity:entity];
    [sortDescriptors release];
    [sortDescriptor release];

    [request setPropertiesToFetch:[NSArray arrayWithObject:@"releaseDate"]];
    NSMutableArray *arrOfdMovies = [[self.managedObjectContxt executeFetchRequest:request error:&error]mutableCopy];
    [self setArrOfFetchdMovies:arrOfdMovies];

    arrOfDistinctYr = [[NSMutableArray alloc]init];
    NSMutableArray *temp = [[NSMutableArray alloc]init];

    for (int i = 0; i<[arrOfdMovies count]; i++) {
             // NSLog(@"%@",[[arrOfdMovies objectAtIndex:i] valueForKey:@"releaseDate"]);
        //date to its components
        NSDate *now = [[arrOfdMovies objectAtIndex:i]valueForKey:@"releaseDate"];
        // Specify which units we would like to use
        unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *component_s = [calendar components:units fromDate:now];

        NSInteger year = [component_s year];
        NSLog(@"%i", year);
//        NSInteger month = [components month];
//        NSInteger day = [components day];
        NSNumber *num= [NSNumber numberWithInteger:year];
        [temp addObject:num];
    [self setArrOfDistinctYr:temp];

    //distinct value logic.
    NSSet *uniqueYrs = [NSSet setWithArray:arrOfDistinctYr];
    arr = [[NSArray alloc]initWithArray:[uniqueYrs allObjects]];

    NSArray *ar = [arr sortedArrayUsingSelector:@selector(compare:)];
    self.arr = ar;
    [self.managedObjectContxt retain];          
    [request release];


the array "ar" contains only years in distinct and sorted manner. But data types of this array elements are "NSNumbr" Now How can i form a predicate which will make use of this array to display records for the particular year(how to check year from date field of core data [which has date say- 2006-01-26]). I tried in following way -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//    return 2;

    NSFetchRequest *requst=[[NSFetchRequest alloc]init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Movies" inManagedObjectContext:self.managedObjectContxt];
    [requst setEntity:entity];
    requst.predicate = [NSPredicate predicateWithFormat:@"releaseDate CONTAINS%@", [arr objectAtIndex:section]];
    NSError *error = nil;
    NSMutableArray *array = [[self.managedObjectContxt executeFetchRequest:requst error:&error]mutableCopy];
    [requst release];
    return [array count];


Once i get this, i can display records n my viewController.

2012-04-04 05:01
by PriyankaJ
some code please.... - iamsult 2012-04-04 05:15
I am new here. Can u plz tell me how to attach code - PriyankaJ 2012-04-04 05:41
You can copy and paste your code into your question. Then highlight your code and click on the { } button. It is located directly above the text box where you are entering your question. Doing so will indent the highlighted area and show it a code - Linger 2012-04-04 15:55


One of the way is - Knowing the year, you could get start of the year 2006-01-01 and end of the year 2006-12-31. And then filter for dates that are between them.

For creating predicates - Have a look at Predicate Guide

It has few good example, including some dealing with NSDate predicates.

2012-04-04 06:27
by NoName