<span id="Label3"></p><p><p>Nsfetchedresultscontroller and UITableView combine to handle data with great Flexibility. The first advantage is that the data records do not need to be paged, otherwise, according to the traditional approach, you need to first query out the total records, and then filtered from the record, so it will be two operations, memory consumption is very large, processing is not good, the program may even Crash. Using the Nsfetchedresultscontroller class is not only simple, but also has a higher performance, This class automatically helps you to record paging things, the table corresponding to the core data object is also very simple.</p></p><p><p>More importantly, When you update or delete records in other interfaces, Nsfetchedresultscontroller can help you synchronize updates uitableview, and your UITableView and database synchronization will become very simple.</p></p><p><p>Here are the delegate methods to implement Nsfetchedresultscontroller</p></p><pre class="prettyprint"><code class=" hljs objectivec"><span class="hljs-comment"><span class="hljs-comment">/* the *nsfetchresultscontroller class is a delegate method that invokes a fetch request and a context as its input and the data in the fetch request changes when the class is called */</span></span>- (<span class="hljs-built_in"><span class="hljs-built_in">Nsfetchedresultscontroller</span></span>*) fetchedresultscontroller{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"fetchedresultscontroller"</span></span>);<span class="hljs-comment"><span class="hljs-comment">//detect If Fetchedresultscontroller has been created</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(_fetchedresultscontroller! =<span class="hljs-literal"><span class="hljs-literal">Nil</span></span>) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>_fetchedresultscontroller; }<span class="hljs-comment"><span class="hljs-comment">/* * You need a fetch request and a context to be able to use Fetchedresultscontroller */</span></span> <span class="hljs-comment"><span class="hljs-comment">//you can view the fetch request as a SQL SELECT statement</span></span>Nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init];<span class="hljs-comment"><span class="hljs-comment">//create an entity based on the event entity in context</span></span>Nsentitydescription *entity = [nsentitydescription entityforname:@<span class="hljs-string"><span class="hljs-string">"Event"</span></span>Inmanagedobjectcontext:<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span><span class="hljs-variable"><span class="hljs-variable">. Managedobjectcontext</span></span>];<span class="hljs-comment"><span class="hljs-comment">//set the entity to be used by Fetchrequest</span></span>[fetchrequest setentity:entity];<span class="hljs-comment"><span class="hljs-comment">//set The batch size of the fetchrequest to a reasonable number of records received by the word</span></span>[fetchrequest setfetchbatchsize:<span class="hljs-number"><span class="hljs-number"></span> -</span>];<span class="hljs-comment"><span class="hljs-comment">//create a nssortdescriptor that uses nssortdescriptor to sort the results of fetchrequest (based on the "timeStamp" field in descending order)</span></span> <span class="hljs-comment"><span class="hljs-comment">//(you can treat nssortdescriptor as a SQL ORDER by Sentence)</span></span>Nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:@<span class="hljs-string"><span class="hljs-string">"timeStamp"</span></span>Ascending:<span class="hljs-literal"><span class="hljs-literal">NO</span></span>];<span class="hljs-built_in"><span class="hljs-built_in">Nsarray</span></span>*sortdescriptors = @[sortdescriptor]; [fetchrequest setsortdescriptors:sortdescriptors];<span class="hljs-comment"><span class="hljs-comment">//requires a FETCH request and a context to be able to use Fetchedresultscontroller</span></span> <span class="hljs-built_in"><span class="hljs-built_in">Nsfetchedresultscontroller</span></span>*afetchedresultscontroller = [[<span class="hljs-built_in"><span class="hljs-built_in">Nsfetchedresultscontroller</span></span>alloc] initwithfetchrequest:fetchrequest managedobjectcontext:<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span><span class="hljs-variable"><span class="hljs-variable">. Managedobjectcontext</span></span>Sectionnamekeypath:<span class="hljs-literal"><span class="hljs-literal">Nil</span></span>cachename:@<span class="hljs-string"><span class="hljs-string">"Master"</span></span>]; Afetchedresultscontroller<span class="hljs-variable"><span class="hljs-variable">. Delegate</span></span>=<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span><span class="hljs-variable"><span class="hljs-variable">. Fetchedresultscontroller</span></span>= afetchedresultscontroller;<span class="hljs-built_in"><span class="hljs-built_in">Nserror</span></span>*error =<span class="hljs-literal"><span class="hljs-literal">Nil</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(! [<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span><span class="hljs-variable"><span class="hljs-variable">. Fetchedresultscontroller</span></span>Performfetch:&error]) {<span class="hljs-comment"><span class="hljs-comment">//Replace This implementation with code to handle the error appropriately.</span></span> <span class="hljs-comment"><span class="hljs-comment">//abort () causes the application to generate a crash log and Terminate. You should don't use the This function in a shipping application, although it could be useful during. </span></span> <span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"unresolved error%@,%@"</span></span>, error, [error userInfo]); Abort (); }<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>_fetchedresultscontroller;}<span class="hljs-comment"><span class="hljs-comment">//method notifies you that "get result controller" will change the internal</span></span>- (<span class="hljs-keyword"><span class="hljs-keyword">void</span></span>) Controllerwillchangecontent: (<span class="hljs-built_in"><span class="hljs-built_in">Nsfetchedresultscontroller</span></span>*) controller{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"controllerwillchangecontent"</span></span>);<span class="hljs-comment"><span class="hljs-comment">//tell The table that an update is about to occur by calling the Beginupdates method of the diagram</span></span>[<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span><span class="hljs-variable"><span class="hljs-variable">. TableView</span></span>beginupdates];}<span class="hljs-comment"><span class="hljs-comment">//</span></span>- (<span class="hljs-keyword"><span class="hljs-keyword">void</span></span>) Controller: (<span class="hljs-built_in"><span class="hljs-built_in">Nsfetchedresultscontroller</span></span>*) Controller didchangesection: (<span class="hljs-keyword"><span class="hljs-keyword">ID</span></span><NSFetchedResultsSectionInfo>) sectioninfo atindex: (nsuinteger) sectionindex forchangetype: (<span class="hljs-built_in"><span class="hljs-built_in">Nsfetchedresultschangetype</span></span>) type{<span class="hljs-keyword"><span class="hljs-keyword">Switch</span></span>(type) {<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span>Nsfetchedresultschangeinsert: [<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span><span class="hljs-variable"><span class="hljs-variable">. TableView</span></span>Insertsections:[nsindexset indexsetwithindex:sectionindex] withrowanimation:uitableviewrowanimationfade];<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span>nsfetchedresultschangedelete: [<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span><span class="hljs-variable"><span class="hljs-variable">. TableView</span></span>Deletesections:[nsindexset indexsetwithindex:sectionindex] withrowanimation:uitableviewrowanimationfade];<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword">default</span></span>:<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>; }}- (<span class="hljs-keyword"><span class="hljs-keyword">void</span></span>) Controller: (<span class="hljs-built_in"><span class="hljs-built_in">Nsfetchedresultscontroller</span></span>*) Controller Didchangeobject: (<span class="hljs-keyword"><span class="hljs-keyword">ID</span></span>) AnObject Atindexpath: (<span class="hljs-built_in"><span class="hljs-built_in">Nsindexpath</span></span>*) Indexpath forchangetype: (<span class="hljs-built_in"><span class="hljs-built_in">Nsfetchedresultschangetype</span></span>) Type Newindexpath: (<span class="hljs-built_in"><span class="hljs-built_in">Nsindexpath</span></span>*) newindexpath{<span class="hljs-built_in"><span class="hljs-built_in">UITableView</span></span>*tableview =<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span><span class="hljs-variable"><span class="hljs-variable">. TableView</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">Switch</span></span>(type) {<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span>Nsfetchedresultschangeinsert: [tableView insertrowsatindexpaths:@[newindexpath] Withrowanimation:uitableviewrow animationfade];<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span>Nsfetchedresultschangedelete: [tableView deleterowsatindexpaths:@[indexpath] Withrowanimation:uitableviewrowani mationfade];<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span>nsfetchedresultschangeupdate: [<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span>Configurecell:[tableview cellforrowatindexpath:indexpath] atindexpath:indexpath];<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span>Nsfetchedresultschangemove: [tableView deleterowsatindexpaths:@[indexpath] Withrowanimation:uitableviewrowanima tionfade]; [tableView insertrowsatindexpaths:@[newindexpath] withrowanimation:uitableviewrowanimationfade];<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>; }}<span class="hljs-comment"><span class="hljs-comment">//notifies you that "get results controller" has completed its changes</span></span>- (<span class="hljs-keyword"><span class="hljs-keyword">void</span></span>) Controllerdidchangecontent: (<span class="hljs-built_in"><span class="hljs-built_in">Nsfetchedresultscontroller</span></span>*) controller{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"controllerdidchangecontent"</span></span>);<span class="hljs-comment"><span class="hljs-comment">//notify Table View update end by calling the Endupdates method</span></span>[<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span><span class="hljs-variable"><span class="hljs-variable">. TableView</span></span>endupdates];}</code></pre> <p><p>IOS---nsfetchresultscontroller</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service