问题描述
我尝试在大型核心数据库(3.2万个对象)中创建搜索,但是当我向 UISearchBar
添加一些文本时,我会有些滞后(因为我的数据库正在更新使用新的搜索谓词)如何解决此问题?我该如何在后台线程中更新我的库?
i try to create search in big Core Data base (32 thousand objects) but when i added some text to the UISearchBar
i have some lags (because my base is updating with new search Predicate) how to fix this? how can i update my base in background thread?
fetchedResultController代码
fetchedResultController code
lazy var context: NSManagedObjectContext = {
let appDelegate = (UIApplication.shared.delegate as? AppDelegate)
let context = appDelegate!.managedObjectContext
return context//appDelegate!.managedObjectContext
}()
lazy var fetchedResultsController: NSFetchedResultsController<CardsBaseClass> = {
let fetchRequest = NSFetchRequest<CardsBaseClass>(entityName: "CardsBase")
let sortDescriptor = NSSortDescriptor(key: "cardName", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
fetchRequest.predicate = nil
fetchRequest.fetchBatchSize = 50
fetchRequest.returnsObjectsAsFaults = false
//let mainContext = (UIApplication.shared.delegate as? AppDelegate)?.managedObjectContext
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.context, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController
}()
并添加一些子MOC作为搜索方法
and add some child MOC for searching methods
lazy var searchMOC: NSManagedObjectContext = {
let moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
moc.parent = self.context
return moc
}()
用于连接fetchedResultController
for connecting fetchedResultController I use
override func viewDidAppear(_ animated: Bool) {
self.view.gestureRecognizers?.removeAll()
connectFetchedRequest()
}
func connectFetchedRequest() {
indicatorLoad.startAnimating()
context.perform({
do {
try self.fetchedResultsController.performFetch()
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
self.indicatorLoad.stopAnimating()
})
} catch {
print(error)
}
})
}
搜索谓词使用以下代码更新
search Predicates are updated with following code
searchMOC.performAndWait({
if searchText != "" {
let predicate = NSPredicate(format: "\(self.searchPredicateName) contains[c] %@", searchText)
predicateArray.append(predicate)
}
self.fetchedResultsController.fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicateArray)
do {
try self.fetchedResultsController.performFetch()
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
} catch {
print(error)
}
})
我有权使用searchMOC.perform并通过DispatchQueue更新tableView吗?
我在核心数据实体中用于谓词的所有属性均已索引,但添加搜索文本后我仍然有很多滞后性
Am I right to use searchMOC.perform and update the tableView via DispatchQueue?All my attributes which I use for Predicates in my Core Data Entity are "indexed", but I still have a lot of lags when added the search text
推荐答案
您必须在子上下文中获取,而不是在主上下文中获取结果控制器。在FRC中执行的任何操作都在主线程上,但是您需要进行后台搜索。
You have to fetch in the child context, not the main context fetched results controller. Whatever you do in the FRC is on the main thread, but you want a background search.
这篇关于通过searchBar过滤NSFetchedResultController而不加载基本暂停?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!