更新fetchedResultsController以获取由UISearchBar设置的谓词 [英] Updating fetchedResultsController for predicate set by UISearchBar

查看:77
本文介绍了更新fetchedResultsController以获取由UISearchBar设置的谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重构为原始 UIViewController

Refactored to vanilla UIViewController

而不是拥有 UITableView 在活动和非活动 UISearchController 之间切换,我将其重构为普通的 UIViewController 顶部的 UISearchBar 和正下方的 UITableView

Rather than having the UITableView toggle between active and inactive UISearchController, I refactored to a vanilla UIViewController with a UISearchBar at the top and a UITableView directly underneath it.

我想做的是在具有由搜索条中的文本设置的谓词和可捕获所有内容的谓词之间切换 fetchedResultsController

What I'm trying to do is toggle the fetchedResultsController between having a predicate which is set by the text in the searchBar and one that grabs everything.

我找到了这个答案,它描述了Objective-C中类似问题的解决方案,并作为重构Swift项目的基础:

I found this answer, which describes a solution to a similar problem in Objective-C and served as the basis upon which I refactored my Swift project:

如何使用UISearchDisplayController / UISearchBar过滤NSFetchedResultsController(CoreData)
https://stackoverflow.com/a / 9512891/4475605

How to filter NSFetchedResultsController (CoreData) with UISearchDisplayController/UISearchBar https://stackoverflow.com/a/9512891/4475605

我的问题:如何更新我的FET searchBar 文本的chedResultsController?

My Problem: How do I update my fetchedResultsController for the searchBar text?

tableView 正确填充,但是我不知道如何为我的 searchPredicate 更新 fetchedResultsController 我已经尝试过了。

The tableView populates correctly, but I can't figure out how to update the fetchedResultsController for my searchPredicate This is what I've tried.

这就是我声明 NSPredicate 的方式。 NSFetchedResultsController 在类的顶部:

Here's how I'm declaring my NSPredicate & NSFetchedResultsController at the top of the class:

var searchPredicate = NSPredicate()

// Create fetchedResultsController to store search results
lazy var fetchedResultsController: NSFetchedResultsController = {
    let fetchRequest = NSFetchRequest(entityName: "Song")
    let sortDescriptor = NSSortDescriptor(key: "songDescription", ascending: true);
    // ** THESE TWO LINES CAUSE A CRASH, BUT NO COMPILER ERRORS **
    // let predicate = self.searchPredicate
    // fetchRequest.predicate = predicate
    fetchRequest.sortDescriptors = [sortDescriptor]
    fetchRequest.fetchBatchSize = 20

    let frc = NSFetchedResultsController (fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: "songDescription", cacheName: nil)

    frc.delegate = self

    return frc
}()

这是我的 UISearchBarDelegate 方法:

// MARK: - UISearchBarDelegate methods
// called when text changes (including clear)
internal func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    print("searchText = \(searchBar.text)")
    searchPredicate = NSPredicate(format: "(songDescription contains [cd] %@) || (songStar contains[cd] %@)", searchBar.text!, searchBar.text!)
    // TODO: figure out how to update fetchedResultsController w/ predicate
}

// called when cancel button pressed
internal func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
    searchBar.text = ""
    // TODO: flip back to normal fetchResultsController
}

这是我的 NSFetchedResultsControllerDelegate 方法

internal func controllerWillChangeContent(controller: NSFetchedResultsController) {
    print("controllerWillChangeContent")
    tableView.beginUpdates()
}

internal func controllerDidChangeContent(controller: NSFetchedResultsController) {
    print("controllerDidChangeContent")
    tableView.reloadData()
    tableView.endUpdates()
}

internal func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    switch type {
    case .Insert:
        tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Automatic)
    case .Delete:
        tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
    case .Update:
        tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
    case .Move:
        tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
        tableView.insertRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
    }
}

感谢您的阅读。我欢迎您的建议。

Thank you for reading. I welcome your suggestions.

推荐答案

这不是那么快的伪代码...它肯定会不编译

This is not-so-swift-pseudo-code... it most assuredly will not compile with any swift compiler.

您的FRC没有使用缓存,因此没有要删除的缓存,我们可以将谓词分配给现有的FRC提取请求

Your FRC is not using a cache, so there is no cache to be deleted, and we can just assign the predicate to the existing FRC fetch request.

您可能不想对搜索栏中的每个字符更改都进行完整的新提取,或者您可能只想对第一个字符进行搜索,然后

You may not want to do a complete new fetch on every character change in the search bar, or you may want to only do the search on the first character, and use subsets for subsequent characters.

// called when text changes (including clear)
internal func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    var predicate:NSPredicate = nil
    if searchBar.text.length != 0 {
        predicate = NSPredicate(format: "(songDescription contains [cd] %@) || (songStar contains[cd] %@)", searchBar.text!, searchBar.text!)
    }
    fetchedResultsController.fetchRequest.predicate = predicate
    fetchedResultsController.performFetch()
    tableView.reloadData()
}

// called when cancel button pressed
internal func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
    searchBar.text = ""
    fetchedResultsController.fetchRequest.predicate = nil
    fetchedResultsController.performFetch()
    tableView.reloadData()
}

这篇关于更新fetchedResultsController以获取由UISearchBar设置的谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆