【问题标题】:UI hangs until all fetches are doneUI 挂起,直到完成所有提取
【发布时间】:2016-07-14 15:52:03
【问题描述】:

我有这段代码来搜索不同的表,我的问题是在执行 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)
                }
            }
        }
    }
}

【问题讨论】:

  • 为什么不先加载 5-10 条内容,然后实现拉取刷新并添加 5-10 条新记录。
  • 大多数搜索的结果少于 10 个,我不想要求用户刷新并等待加载 2 条或可能 0 条记录。
  • 然后使用 dispatch_async(dispatch_get_main_queue(), { })

标签: ios swift user-interface core-data tableview


【解决方案1】:

【讨论】:

  • 尝试了异步获取,但 tableview 在完成对所有表的搜索后刷新。
【解决方案2】:

UI 在主线程中处理其大部分进程。如果您有一个主要进程,请不要在主线程上同步运行它。您可以在主线程上异步运行它。

在主线程上异步运行的代码:

dispatch_async(dispatch_get_main_queue(), {

    // RUN CODE OVER HERE

})

【讨论】:

  • 我的主要过程是获取并在不同的线程中运行(“managedContext.performBlock”),您提供给我的代码是访问主线程并更新 UI。
猜你喜欢
  • 2020-06-05
  • 2011-05-23
  • 2013-09-17
  • 1970-01-01
  • 2015-06-10
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多