IOS --- NSFetchResultsController

Source: Internet
Author: User

IOS --- NSFetchResultsController

NSFetchedResultsController and UITableView are integrated to process data with great flexibility. The first advantage is that data records do not need to be paged. Otherwise, according to the traditional practice, you need to first query the total records and then filter them from the records, in this way, two operations will be performed, causing a high memory consumption, poor processing, and the program may even crash. The NSFetchedResultsController class is not only simple, but also has higher performance. This class automatically helps you record paging tasks and obtain the Core Data object corresponding to the table.

More importantly, when you update or delete records on other interfaces, NSFetchedResultsController can help you synchronously update UITableView, and it will be very easy to synchronize your UITableView and database.

The following is the delegate method for implementing NSFetchedResultsController:

/** The NSFetchResultsController class uses a GET request and a context as its input and calls the delegate method of this class when the data in the GET request changes */-(NSFetchedResultsController *) fetchedResultsController {NSLog (@ "fetchedResultsController"); // checks whether fetchedResultsController has been created if (_ fetchedResultsController! = Nil) {return _ fetchedResultsController ;} /** you need a GET request and a context to use fetchedResultsController * // You can regard the GET request as an SQL SELECT statement NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init]; // create an entity NSEntityDescription * entity = [NSEntityDescription entityForName: @ "Event" inManagedObjectContext: self. managedObjectContext]; // sets the object to use [fetchRequest setEntity: entity] By fetchRequest; // sets fetchRequ The batch size of est is the reasonable number of records received by words [fetchRequest setFetchBatchSize: 20]; // create an NSSortDescriptor, use NSSortDescriptor to sort fetchRequest results (sort the results in descending ORDER based on the "timeStamp" field) // (you can regard NSSortDescriptor as the SQL ORDER BY Statement) optional * sortDescriptor = [[delealloc] initWithKey: @ "timeStamp" ascending: NO]; NSArray * sortDescriptors = @ [sortDescriptor]; [fetchRequest setSortDescriptors: sortDescriptors] // a GET request and a context are required to use fe TchedResultsController NSFetchedResultsController * aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest: fetchRequest managedObjectContext: self. managedObjectContext sectionNameKeyPath: nil cacheName: @ "Master"]; aFetchedResultsController. delegate = self; self. fetchedResultsController = aFetchedResultsController; NSError * error = nil; if (! [Self. fetchedResultsController implements mfetch: & error]) {// Replace this implementation with code to handle the error appropriately. // abort () causes the application to generate a crash log and terminate. you shoshould not use this function in a shipping application, although it may be useful during development. NSLog (@ "Unresolved error % @, % @", error, [error userInfo]); abort () ;}return _ fetchedResultsController ;} // The method notifies you that "get result controller" will change the same-(void) controllerWillChangeContent :( NSFetchedResultsController *) controller {NSLog (@ "controllerWillChangeContent "); // call beginUpdates to indicate that the table update is about to happen. tableView beginUpdates];} //-(void) controller :( NSFetchedResultsController *) controller didChangeSection :( id
  
   
) SectionInfo atIndex :( NSUInteger) sectionIndex forChangeType :( NSFetchedResultsChangeType) type {switch (type) {case NSFetchedResultsChangeInsert: [self. tableView insertSections: [NSIndexSet indexSetWithIndex: sectionIndex] withRowAnimation: UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: [self. tableView deleteSections: [NSIndexSet indexSetWithIndex: sectionIndex] withRowAnimation: break]; break; default: return ;}}-(void) controller :( NSFetchedResultsController *) controller didChangeObject :( id) anObject atIndexPath :( NSIndexPath *) indexPath forChangeType :( NSFetchedResultsChangeType) type newIndexPath :( NSIndexPath *) newIndexPath {UITableView * tableView = self. tableView; switch (type) {case when: [tableView insertRowsAtIndexPaths: @ [newIndexPath] withRowAnimation: Signature]; break; case when: [tableView deleteRowsAtIndexPaths: @ [indexPath] withRowAnimation: break]; break; case when: [self configureCell: [tableView cellForRowAtIndexPath: indexPath] atIndexPath: indexPath]; break; case when: [tableView deleteRowsAtIndexPaths: @ [indexPath] withRowAnimation: response]; [tableView insertRowsAtIndexPaths: @ [newIndexPath] withRowAnimation: UITableViewRowAnimationFade]; break ;}// notify you that the "get result controller" has been changed-(void) controllerDidChangeContent :( NSFetchedResultsController *) controller {NSLog (@ "controllerDidChangeContent"); // call the endUpdates method to notify the table view that update ends [self. tableView endUpdates];}
  

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.