gpt4 book ai didi

ios - UI 挂起,直到完成所有提取

转载 作者:行者123 更新时间:2023-11-28 06:39:23 25 4
gpt4 key购买 nike

我有这段代码可以搜索不同的表,我的问题是在执行 las fetch 请求之前我无法与 UI 交互。如果我搜索特定值并且结果在“Table2”中,则 tableView 更新正常但无法与其交互,直到它完成对最后一个表的搜索。 func loadData() 只需要几毫秒的时间来执行和退出,并且提取在不同的线程中执行。我不知道这段代码有什么问题,有什么帮助或建议吗?所有表中的记录总数约为 500 万条,搜索所有这些记录需要一些时间,这就是为什么我不想让用户在完成搜索整个数据库之前等待一些结果可用。

func loadData () {
let tablas = ["Table1", "Table2", "Table3", "Table4", "Table5", "Table6", "Table7", "Table8", "Table9", "Table10", "Table11"]
let managedContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
managedContext.parentContext = self.moc
for tbl in tablas {
managedContext.performBlock {
let fetchRequest = NSFetchRequest(entityName: tbl)
let predicate = NSPredicate(format: "name CONTAINS %@", self.filter)
fetchRequest.predicate = predicate
fetchRequest.resultType = NSFetchRequestResultType.ManagedObjectIDResultType
fetchRequest.fetchLimit = 50
fetchRequest.fetchBatchSize = 10
do {
let results = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObjectID]
if results.count != 0 {
self.resultArray.appendContentsOf(results)
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
} catch let error as NSError {
dispatch_async(dispatch_get_main_queue()) {
let errorAlert = UIAlertController(title: "Error!!!", message: error.localizedDescription, preferredStyle: .Alert)
errorAlert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
self.presentViewController(errorAlert, animated: true, completion: nil)
}
}
}
}
}

最佳答案

关于ios - UI 挂起,直到完成所有提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38378807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com