IOS Filter NSFetchedResultsController Effectively

I am trying to add search support to the Master Data UITableView and started to hit some performance issues. I currently have associations with two NSFetchedResultsController(one for regular and one for searching). In my methods, UISearchDisplayDelegateI release and recreate mine NSFetchedResultsControllerafter each call. However, this causes a delay in typing! Is there a more efficient way to filter NSFetchedResultsController? I set the batch size to about 50 items, and my database has several thousand records, if that matters. Thank!

+3
source share
3 answers

The documentation makes this pretty clear: NSFetchedResultsController

  • , .
  • fetchRequest. , ; .
  • -executeFetch:.
+4

FetchedResultController

NSPredicate *pre = [NSPredicate predicateWithFormat:@"attribute CONTAINS [cd] %@", searchString];
NSArray *searchResults = [[self.fetchedResultsController fetchedObjects] filteredArrayUsingPredicate:pre]

, !

+3

Yes, you can:

Create an instance of NSFetchRequest and change its sort handle every time:

 let shortDescriptor = NSSortDescriptor(key: key, ascending: ascending)
    request.sortDescriptors = [shortDescriptor]

    do {
        try fetchedResultViewController?.performFetch()
    } catch let error as NSError {
        print("Error in fetch \(error)")
    }

Read the apple doc: https://developer.apple.com/reference/coredata/nsfetchedresultscontroller

0
source

Source: https://habr.com/ru/post/1789174/


All Articles