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

时间:2019-07-06 21:41:21

标签: ios swift nspredicate nsfetchedresultscontroller

在尝试搜索之前,我的数据加载正常,但是当我开始在搜索栏中输入内容时,调用try fetchedRC.performFetch()

时会崩溃

我尝试使用Google搜索并更改代码,但是似乎没有任何效果。

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

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)

并且要注意,我还知道在request.predicate中list.name的位置是不合适的,我只是作为占位符希望这样做会更容易理解

0 个答案:

没有答案