iphone - Core Data - Sorting records that are fetched as part of relationship -


i have following entities....

person
| --- review

i retrieve list of person records .... , directly able reference review records via person.reviews (which nice , easy without having write nsfetch statements.)

my question how sort reviews date (there date attribute within review entity) ? when reference person.reviews in date order ?

my temp fix sort array outside of fetch

nsarray *sortdescriptors = [nsarray arraywithobject:[nssortdescriptor sortdescriptorwithkey:@"startdate" ascending:yes]];  nsarray *reviewlist; reviewlist = [[[nsarray alloc] initwitharray:[person.reviews allobjects]] sortedarrayusingdescriptors:sortdescriptors]; 

many thanks

assuming mean want fetch list of review entities given person, sorted date:

you can use nsfetchrequest, nspredicate , nssortdescriptor filtered core data fetch sorting of results.

example:

nsmanagedobjectcontext *context = <#get context#>;  nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"review"     inmanagedobjectcontext:context]; [fetchrequest setentity:entity];  nspredicate *predicate = [nspredicate predicatewithformat:@"person == %@", theperson]; [fetchrequest setpredicate:predicate];  nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"startdate"     ascending:yes]; nsarray *sortdescriptors = @[sortdescriptor]; [fetchrequest setsortdescriptors:sortdescriptors];  nserror *error; nsarray *fetchedobjects = [context executefetchrequest:fetchrequest error:&error]; if (fetchedobjects == nil) {     // handle error. } 

the above code assumes review entity has date field called startdate , field called person link person.

after code has run, fetchedobjects contains review objects sorted review date order (and reviews objects person of interest).

for more info, please read https://developer.apple.com/library/ios/documentation/datamanagement/conceptual/coredatasnippets/articles/fetching.html


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -