开发者

iOS Filter NSFetchedResultsController Efficiently

I'm attempting to add search support to a Core Data backed UITableView开发者_开发百科 and started to hit some performance issues. I currently have associations to two NSFetchedResultsController (one for regular and one for searching). In my UISearchDisplayDelegate methods I release and recreate my NSFetchedResultsController after each call. However, this causes lag while typing! Is there a more efficient way to filter an NSFetchedResultsController? I've setup my batch size to around 50 items and my database has a few thousand records if that makes a difference. Thanks!


The NSFetchedResultsController documentation makes it pretty clear:

  • If there is a cache, delete it.
  • Update the fetchRequest property. Note that you cannot simply modify the existing fetch request; you must create a new one.
  • Call -executeFetch:.


I use the following code to filter the results of the FetchedResultController

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

Hope that helps!


Yes you can:

Create the NSFetchRequest instance and change its sort descriptor each 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 document: https://developer.apple.com/reference/coredata/nsfetchedresultscontroller

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜