Reputation: 454
Hi i am new to this coredata. I have two entities CategoryList and ExpenseList. CategoryList consists of two attributes
1.dates (nsdate)
2.categories (nsstring)
and in to many relation with ExpenseList, relationship name is "expenseList"
ExpenseList consists of
1.amountSpent (float).
I just wanted to know learn about relationship features in coredata. When i tried to create a nspredicate to filter the data by a specific date, its returning nothing. I just wanted to filter the categories attribute content in the CategoryList entity for a specific date. Here is my code
-(void) callingByDate
{
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSEntityDescription *categoryL = [NSEntityDescription entityForName:@"CategoryList" inManagedObjectContext:managedObjectContext];
NSSortDescriptor *dateSort = [[NSSortDescriptor alloc]initWithKey:@"date" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:dateSort, nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dates == %@",date];
NSFetchRequest *dateFetch = [[NSFetchRequest alloc]init];
[dateFetch setSortDescriptors:sortDescriptors];
[dateFetch setEntity:categoryL];
[dateFetch setPredicate:predicate];
NSMutableArray *results = [[managedObjectContext executeFetchRequest:dateFetch error:nil] mutableCopy];
if([results count] > 0)
NSLog(@"results found");
else
NSLog(@"no results found");
}
when i execute this code in the nslog area its showing 'no results found'. I could not figure out why . Thanks in advance
here is my code to find the end and start of the day
-(NSDate *)beginningOfDay:(NSDate *)date;
{
NSDate *dat = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:dat];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
-(NSDate *)endOfDay:(NSDate *)date;
{
NSDate *dat = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:dat];
[components setHour:23];
[components setMinute:59];
[components setSecond:59];
return [cal dateFromComponents:components];
when i nslog, the result showing is startday -2013-04-30 18:30:00 +0000 endOfDay -2013-05-01 18:29:59 +0000
Upvotes: 3
Views: 5184
Reputation: 387
i am using this solution for comparing only date without time in this case my date format is yyyy-MM-dd:
NSDateFormatter * formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString * dateString = [formatter stringFromDate:[NSDate date];
NSDate *rdate = [formatter dateFromString:dateString];
NSPredicate *datePre = [NSPredicate predicateWithFormat:@"date = %@", rdate];
You can use date and time both for changing format of date.
Upvotes: 0
Reputation: 2759
date
is actually a timestamp, so doing ==
with it will only return items which have that exact timestamp.
To get all the entries on a specific date you'll want to get the NSDate
for the start of the day (startOfDay
), and the NSDate
for the end of the day (endOfDay
) then modify the predicate to get dates between those two values using:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dates >= %@ AND dates >= %@", startOfDay, endOfDay];
To calculate the startOfDay
and endOfDay
you can either do it yourself manually or there are some other libraries you can use to calculate it such as https://github.com/erica/NSDate-Extensions.
Upvotes: 9