我一直在浏览众多的论坛和教程,希望加快我的cloudkit应用程序 - (至少)似乎答案归结为将服务质量功能设置为“UserInteractive”。目前,用3条记录更新一个表控制器大约需要2/3秒,每条记录只有一个字符串变量,通过标签显示。据我所知,将qos设置为用户交互将强制UI更快地更新。但是,我不确定如何正确地实现qos代码。就目前而言,这是我的代码需要2/3秒才能加载。
func fetch()
{
let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Dining", predicate: predicate)
publicDatabase.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in
if (error != nil)
{
print("Error" + (error?.localizedDescription)!)
}
else
{
for result in results!
{
self.categories.append(result)
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
}
}
}
fetch()
}
因此,为了实现更快的UI响应,我能够实现这样的事情吗?
func fetch()
{
let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Dining", predicate: predicate)
publicDatabase.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in
if (error != nil)
{
print("Error" + (error?.localizedDescription)!)
}
else
{
for result in results!
{
self.categories.append(result)
}
let qualityOfServiceClass = QOS_CLASS_UserInteractive
let Queue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(Queue, {
print("This is run on the background queue")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
})
}
}
}
fetch()
}
答案 0 :(得分:0)
沿着这些方向使用查询操作:
func fetch(){
let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Dining", predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
queryOperation.qualityOfService = .userInteractive
queryOperation.recordFetchedBlock = {(record: CKRecord) -> Void in
// Do things with record
queryOperation.queryCompletionBlock = {(cursor, error) in
// Handle error and do things
}
}
publicDB.add(queryOperation)
}
请参阅有关NSOperation及其实例属性QualityofService
的文档