ios - coredata filtering by date using NSPredicate returns nothing -
hi new coredata. have 2 entities categorylist , expenselist. categorylist consists of 2 attributes
1.dates (nsdate)
2.categories (nsstring)
, in many relation expenselist, relationship name "expenselist" expenselist consists of 1.amountspent (float).
wanted know learn relationship features in coredata. when tried create nspredicate filter data specific date, returning nothing. wanted filter categories attribute content in categorylist entity specific date. here 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 execute code in nslog area showing 'no results found'. not figure out why . in advance
here code find end , start of 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 nslog, result showing startday -2013-04-30 18:30:00 +0000 endofday -2013-05-01 18:29:59 +0000
date
timestamp, doing ==
return items have exact timestamp.
to entries on specific date you'll want nsdate
start of day (startofday
), , nsdate
end of day (endofday
) modify predicate dates between 2 values using:
nspredicate *predicate = [nspredicate predicatewithformat:@"dates >= %@ , dates >= %@", startofday, endofday];
to calculate startofday
, endofday
can either manually or there other libraries can use calculate such https://github.com/erica/nsdate-extensions.
Comments
Post a Comment