objective c - Fetch from Core Data sorted by formatted date -


i having trouble fetching results core data sorted date.

i have db table contains football matches. each match has hometeam, awayteam , kickofftime. kickofftime nsdate stores date , time match start.

i want display results of query in tableview divided sections kickoff date. date section heading.

this little more complex might first appear. due differing time zones match starting on 1 date in 1 part of world starting on different date in part of world. can't ignore times , store kickoff dates in column.

what i'm trying create custom accessor returns formatted date, in whatever time zone user in, , use sort , section results. here's code in match.h:

@dynamic kickofftime; @dynamic formattedkickofftime; @dynamic dateformatter;  - (nsstring *)formattedkickofftime {     [self willaccessvalueforkey:@"kickofftime"];      // set date formatter format want display date     [dateformatter setdateformat:@"ccc, d mmm"];      // format date     nsstring *myformattedkickofftime = [dateformatter stringfromdate:[self kickofftime]];      [self didaccessvalueforkey:@"kickofftime"];      // return formatted date     return myformattedkickofftime; }  - (nsdateformatter *)dateformatter  {        if (dateformatter == nil)      {         dateformatter = [[nsdateformatter alloc] init];     }     return dateformatter; }  @end 

however when try fetch , sort data so:

nssortdescriptor *kickoffdescriptor = [[nssortdescriptor alloc] initwithkey:@"formattedkickofftime" ascending:yes]; ... nsfetchedresultscontroller *afetchedresultscontroller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetchrequest managedobjectcontext:appdelegate.managedobjectcontext sectionnamekeypath:@"formattedkickofftime" cachename:nil]; 

i following error message:

*** terminating app due uncaught exception 'nsinvalidargumentexception', reason: 'keypath formattedkickofftime not found in entity <nssqlentity match id=1>' 

would offer advice please?

so i've found way this. first of keep dates , calculations in model in gmt. add custom accessor (kickoffdate) match entity gets kickofftime, sets it's time 00:00:00 , returns this. 1 thing had do, wasn't obvious, set timezone of date returned custom accessor gmt otherwise being set user's system timezone.

as dates returned model in gmt seems work should no matter timezone set in iphone's settings. guess core data smart enough timezone adjustments before puts matches in sections.

i have 1 question though, [nstimezone timezoneforsecondsfromgmt:0] best way 0 timezone, mean timezone isn't affected daylight saving time?

oh , have call [self willaccessvalueforkey:@"kickofftime"]; , [self didaccessvalueforkey:@"kickofftime"];? don't understand these doing!

here's code in match.h:

... @dynamic hometeam; @dynamic awayteam; @dynamic kickofftime;  - (nsdate *)kickoffdate {     [self willaccessvalueforkey:@"kickofftime"];     nscalendar *cal = [nscalendar currentcalendar];     nsdatecomponents *components = [cal components:(nsyearcalendarunit|nsmonthcalendarunit|nsdaycalendarunit) fromdate:[self primitivevalueforkey:@"kickofftime"]];     [components sethour:0];     [components setminute:0];     [components setsecond:0];     [components settimezone:[nstimezone timezoneforsecondsfromgmt:0]];      [self didaccessvalueforkey:@"kickofftime"];     return [cal datefromcomponents:components]; } 

and fetch code includes following sort on kickofftime , sectionnamekeypath set kickoffdate:

nssortdescriptor *kickoffdescriptor = [[nssortdescriptor alloc] initwithkey:@"kickofftime" ascending:yes]; ... nsfetchedresultscontroller *afetchedresultscontroller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetchrequest managedobjectcontext:appdelegate.managedobjectcontext sectionnamekeypath:@"kickoffdate" cachename:nil]; 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -