When I first started using coredata, nsfetchedresultscontroller was often used in programming. I didn't know much about the usage of this class at first, but I had a little bit of experience when I checked the official instruction documents, so now I will write it down and share it with you.
Nsfetchedresultscontroller is used to efficiently obtain data in coredata, and then the obtained data is actually uitableview;
The xcdatamodeld file generated by coredata is used for persistent data persistence, that is, the data stored in xcdatamodeld after shutdown will not be lost. If the online request data is then saved to the xcdatamodeld file, the nsfetchedresultscontroller will automaticallyNsmanagedobjectcontext * context updates the data and refresh the list;
That is, nsfetchedresultscontroller will automatically update the context content and make it realistic. It also serves as an interface for providing coredata to the user, with the following code posted:
-(Void) setfetchedresultscontroller :( nsfetchedresultscontroller *) newfrc {nsfetchedresultscontroller * oldfrc = _ fetchedresultscontroller; If (newfrc! = Oldfrc) {_ fetchedresultscontroller = newfrc; newfrc. Delegate = self; If ((! Self. Title | [self. Title isw.tostring: oldfrc. fetchrequest. entity. Name]) & (! Self. navigationcontroller |! Self. navigationitem. title) {self. title = newfrc. fetchrequest. entity. name;} If (newfrc) {If (self. debug) nslog (@ "[% @] % @", nsstringfromclass ([self class]), nsstringfromselector (_ cmd), oldfrc? @ "Updated": @ "set"); [self defined mfetch];} else {If (self. debug) nslog (@ "[% @] Reset to nil", nsstringfromclass ([self class]), nsstringfromselector (_ cmd); [self. tableview reloaddata] ;}}# Pragma mark-uitableviewdatasource-(nsinteger) numberofsectionsintableview :( uitableview *) tableview {nsinteger sections = [[self. fetchedresultscontroller sections] Count]; return sections;}-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {nsinteger rows = 0; If ([self. fetchedresultscontroller sections] Count]> 0) {id <nsfetchedresultssectioninfo> sectioninfo = [[self. fetchedresultscontroller sections] objectatindex: section]; rows = [sectioninfo numberofobjects];} return rows;}-(nsstring *) tableview :( uitableview *) tableview titleforheaderinsection :( nsinteger) section {return [[self. fetchedresultscontroller sections] objectatindex: section] Name];}-(nsinteger) tableview :( uitableview *) tableview sectionforsectionindextitle :( nsstring *) Title atindex :( nsinteger) index {return [self. fetchedresultscontroller sectionforsectionindextitle: Title atindex: Index];}-(nsarray *) sectionindextitlesfortableview :( uitableview *) tableview {return [self. fetchedresultscontroller sectionindextitles];} # pragma mark-delegate // when context changes the content, execute the Delegate-(void) controllerwillchangecontent :( nsfetchedresultscontroller *) controller {[self. tableview beginupdates];} // when this part is to be modified, execute the Delegate-(void) controller :( nsfetchedresultscontroller *) controller didchangesection :( id <nsfetchedresultssectioninfo>) sectioninfo atindex :( NSU) sectionindex forchangetype :( nsfetchedresultschangetype) type {Switch (type) // judge whether to add or delete {nslog (@ "sectionindex is % d", sectionindex); Case nsfetchedresultschangeinsert: [self. tableview insertsections: [nsindexset indexsetwithindex: sectionindex] withrowanimation: uitableviewrowanimationfade]; break; Case nsfetchedresultschangedelete: [self. tableview deletesections: [nsindexset indexsetwithindex: sectionindex] withrowanimation: Expiration]; break ;}// when the content object is changed, execute-(void) controller :( nsfetchedresultscontroller *) controller didchangeobject :( ID) anobject atindexpath :( nsindexpath *) indexpath forchangetype :( nsfetchedresultschangetype) type newindexpath :( nsindexpath *) newindexpath {Switch (type) {case when: [self. tableview insertrowsatindexpaths: [nsarray arraywithobject: newindexpath] withrowanimation: uitableviewrowanimationfade]; break; Case nsfetchedresultschangedelete: [self. tableview deleterowsatindexpaths: [nsarray arraywithobject: indexpath] withrowanimation: uitableviewrowanimationfade]; break; Case nsfetchedresultschangeupdate: [self. tableview reloadrowsatindexpaths: [nsarray arraywithobject: indexpath] withrowanimation: uitableviewrowanimationfade]; break; Case nsfetchedresultschangemove: [self. tableview deleterowsatindexpaths: [nsarray arraywithobject: indexpath] withrowanimation: uitableviewrowanimationfade]; [self. tableview metadata: [nsarray arraywithobject: newindexpath] withrowanimation: plaintext]; break ;}// when the content is modified, execute-(void) controllerdidchangecontent :( optional *) controller {[self. tableview endupdates];}
Usage of nsfetchedresultscontroller