gpt4 book ai didi

ios - CloudKit 响应时间慢?

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

我正在尝试从 Parse 切换到 CloudKit,到目前为止我的示例可以正常工作。我有一个看起来像这样的 CLoudKitModel:

class CloudKitModel {
let container: CKContainer
let privateDB: CKDatabase
let publicDB: CKDatabase

init() {
container = CKContainer.defaultContainer()
privateDB = container.privateCloudDatabase
publicDB = container.publicCloudDatabase
}

func getPlayers(completionHandler: (data: [CloudKitPlayers]?, success: Bool) -> ()) {
var playerArray = [CloudKitPlayers]()
let predicate = NSPredicate(value: true)
let sort = NSSortDescriptor(key: "sortID", ascending: true)
let query = CKQuery(recordType: "Player", predicate: predicate)
query.sortDescriptors = [sort]

let operation = CKQueryOperation(query: query)

operation.recordFetchedBlock = { (record) in
let players = CloudKitPlayers()
players.age = record["age"] as! Int
players.name = record["name"] as! String
players.nickname = record["nickname"] as! String
players.position = record["position"] as! String

let imageData = record["image"] as! CKAsset
players.image = UIImage(contentsOfFile: imageData.fileURL.path!)

playerArray.append(players)
}

operation.queryCompletionBlock = { (cursor, error) in
if error == nil {
print("Fetched all players")
if playerArray.count > 0 {
completionHandler(data: playerArray, success: true)
} else {
completionHandler(data: nil, success: false)
}
}
}

self.publicDB.addOperation(operation)
}
}

我在 UITableViewController 中调用这个 getPlayers(completionHandler: ) 函数:

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

let cloudKit = CloudKitModel()
cloudKit.getPlayers { (data, success) in
if success {
if let playerArray = data {
self.players = playerArray
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
print("reloaded tableView")
}
} else {
print("no success")
}
}
}

问题是:在 TableView 中显示名称平均需要大约 10 秒。这并不是说我一次得到一千条记录,这些只是我从 iCloud 加载的 8 个数据集。我读到我应该在主线程上执行 tableView.reloadData(),但这也没有帮助。

是否还有其他我遗漏的东西或如何调试它的特定方法?与 Parse 相比,这个解决方案慢了 10 倍......

感谢任何想法!

最佳答案

你能试试设置QOS吗?

operation.qualityOfService = .UserInitiated

关于ios - CloudKit 响应时间慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36870939/

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