ios - 在更新 TableView 时,如何使用搜索栏对 NSFetchedResults 数据进行排序,而不会在 fetchedRC.performFetch() 上崩溃?更新

标签 ios swift uitableview nsfetchedresultscontroller uisearchcontroller

在尝试搜索之前,我的数据加载正常,但一旦我开始在搜索栏中输入内容,它就会在调用 try fetchedRC.performFetch() 时崩溃。

我尝试过谷歌搜索并更改代码,但似乎没有任何效果。

这是我从与 CoreData 中的数据的一对多关系获取数据的设置。

func setupFetchedResultsController() {
    //fetchedRC = nil
    let request = Item.fetchRequest() as NSFetchRequest<Item>

    if !currentSearchText.isEmpty && !(currentSearchText == " ") {
        request.predicate = NSPredicate(format: "list.name CONTAINS[c] \(currentSearchText)", parentObj)
    } else {
        request.predicate = NSPredicate(format: "list = %@", parentObj)
    }

    //list CONTAINS[c] \(currentSearchText)
    let sort = NSSortDescriptor(key: #keyPath(Item.isComplete), ascending: true)

    request.sortDescriptors = [sort]
    do {
        fetchedRC = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
        try fetchedRC.performFetch()
        self.tableView.reloadData()
    } catch let error as NSError {
        print(error.localizedDescription)
    }
    fetchedRC.delegate = self
}

这是我的 updateSearchResults 函数

func updateSearchResults(for searchController: UISearchController) {
    guard let text = searchController.searchBar.text else { return }
    currentSearchText = text
    //fetchedRC = nil
    setupFetchedResultsController()
}

我已经设置了它,因此有一个父列表和许多子项目。我期望发生的是让子项显示包含用户在搜索栏中输入的内容。因此,当用户在搜索栏中输入更多内容时,它会更新并优化搜索。实际发生的情况是,它在调用try fetchedRC.performFetch()时崩溃了。 。有什么办法可以解决这个问题呢?我也是刚开始使用 FetchedResultsController 来执行此操作。

这也是它崩溃时给出的错误消息:CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLFetchRequestContext: 0x28180cee0> , unimplemented SQL generation for predicate : (list CONTAINS[c] A) (LHS and RHS both keypaths) with userInfo of (null)

作为注释,我也知道它在哪里说 list.namerequest.predicate ,是不正确的,我只是作为占位符这样做,希望它会更容易理解。

最佳答案

我找到了问题的解决方案,希望这也能帮助其他人解决这个问题。

func setupFetchedResultsController() {

    let request = Item.fetchRequest() as NSFetchRequest<Item>

    //This line simplified just to help it be more reliable
    if (currentSearchText.count > 0) {
        // Line Updated and Fixed Problem
        request.predicate = NSPredicate(format: "list = %@ && name CONTAINS %@", parentObj, currentSearchText)
    } else {
        request.predicate = NSPredicate(format: "list = %@", parentObj)
    }

    let sort = NSSortDescriptor(key: #keyPath(Item.isComplete), ascending: true)

    request.sortDescriptors = [sort]

    do {
        fetchedRC = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
        try fetchedRC.performFetch()
        self.tableView.reloadData()
    } catch let error as NSError {
        print(error.localizedDescription)
    }

    fetchedRC.delegate = self
}

关于ios - 在更新 TableView 时,如何使用搜索栏对 NSFetchedResults 数据进行排序,而不会在 fetchedRC.performFetch() 上崩溃?更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57184861/

相关文章:

ios - 在 ViewController 上居中 subview

ios - 重复淡入淡出效果不起作用

ios - 为什么 AVAudioSessionMediaServicesWereResetNotification 经常发生(iOS 10)?

ios - MKCircle-添加/删除覆盖

ios - 当应用程序进入前台时如何重新加载 UITableView 数据? (Xcode 8.3/swift 3)

ios - 如何在 swift 中仅共享内置 iOS 应用程序

swift - 支持 iOS 12 和 13 时的 UITableView 和 Diffable 数据源

swift - 如何在 SWIFT 的 UITableViewController 中切换单元格?

ios - 调整播放器窗口的大小

iPhone iOS 如何合并 Core Data NSManagedObjectContext?