ios - Fetch arrays of values in CoreData -
in xcode project, have 2 entities coredata. these entities called session , sessiondetails. session has 1 attribute called timecompleted (type: date)
time stamp of event. session in one-to-many relationship sessiondetails attribute important averagedistance (type: float)
.
the sessions sorted using timecompleted. fetch averagedistance values of sorted sessions , place them in array used generate plot. x-axis have date of event , y-axis have averagedistance value. not concerned right creating plot rather extracting values used it. appreciate help!
you can use key-value coding array of attribute values. assuming averagedistance
attribute of session, can following:
nsarray *sessions = ...; // sorted array of session objects nsarray *distvalues = [sessions valueforkey:@"averagedistance"];
distvalues
array of nsnumber
objects containing averagedistance
each session object.
update: turned out in discussion, there to-many relationship session sessiondetails, averagedistance
attribute of sessiondetails, , need possible (timecompleted, averagedistance)
pairs, sorted timecompleted
.
therefore, should fetch sessiondetails objects instead of fetching session objects, , sort them according timecompleted
of related session:
nsfetchrequest *request = [nsfetchrequest fetchrequestwithentityname:@"sessiondetails"]; nssortdescriptor *sort = [nssortdescriptor sortdescriptorwithkey:@"session.timecompleted" ascending:yes]; [request setsortdescriptors:@[sort]]; nsarray *result = [context executefetchrequest:request error:&error];
(i have assumed have inverse relationship session
sessiondetails session.)
now can extract timecompleted
, averagedistance
value these objects, using key-value coding:
nsarray *xvalues = [result valueforkeypath:@"session.timecompleted"]; nsarray *yvalues = [result valueforkey:@"averagedistance"];
Comments
Post a Comment