ios - NSFetchedResultsController from multiple entities and updating model -
i have existing core data model has entity video. want update app , add entity object called project. seems achieved using core data light migration.
now video child of project. , in uitableview display projects section headers , videos rows.
what best way accomplish it? using nsfetchedresultscontroller query core data. thank you
if i'm not mistaken, can achieve kind of change using lightweight migration. have create one-to-many ordered relationship between project entity , video entity. can still use nsfetchedresultscontroller fetch list of projects , traverse relationship video entity associated objects. more or less this:
nsentitydescription *entity = [nsentitydescription entityforname:@"project" inmanagedobjectcontext: context]; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; [fetchrequest setentity:entity]; [fetchrequest setrelationshipkeypathsforprefetching: @"videos"]; nsfetchedresultscontroller *controller = [[nsfetchedresultscontroller alloc] initwithfetchrequest: fetchrequest managedobjectcontext: context sectionnamekeypath: nil cachename: nil];
we're setting nsfetchrequest object prefetch "videos" relationship save time when accessing video entities. then, after retrieving list of project entities, access them in tableview:cellforrowatindexpath:
- (nsinteger) numberofsectionsintableview: (uitableview*) tableview { return [self.fetchedresultscontroller.fetchedobjects count]; } - (nsinteger) tableview: (uitablview*) tableview numberofrowsinsection: (nsinteger) section { project *project = [self.fetchedresultscontroller.fetchedobjects objectatindex: section]; return [project.videos count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { ... project *project = [self.fetchedresultscontroller.fetchedobjects objectatindex: indexpath.section]; video *video = [project.videos objectatindex: indexpath.row]; ... }
Comments
Post a Comment