简体   繁体   English

CloudKit 响应时间慢?

[英]CloudKit slow response time?

I'm trying to switch from Parse to CloudKit and got my sample working so far.我正在尝试从 Parse 切换到 CloudKit 并且到目前为止我的示例工作正常。 I got a CLoudKitModel which looks like this:我得到了一个 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)
    }
}

and I call this getPlayers(completionHandler: ) function in an UITableViewController :我在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")
        }
    }
}

The problem is: It takes on average about 10 seconds to display the names in the TableView.问题是:在TableView 中显示名称平均需要10 秒左右。 It's not that I'm getting like a thousand records at once, these are just 8 data-sets that I'm loading from iCloud.并不是说我一次得到一千条记录,这些只是我从 iCloud 加载的 8 个数据集。 I read that I should perform the tableView.reloadData() on the main thread, but that didn't help either.我读到我应该在主线程上执行tableView.reloadData() ,但这也没有帮助。

Is there something else that I'm missing or a particular way how to debug this?是否还有其他我遗漏的东西或如何调试它的特定方法? Compared the Parse this solution is like 10x slower...与 Parse 相比,这个解决方案慢了 10 倍......

Thanks for any ideas!感谢您的任何想法!

您可以尝试设置QOS吗?

operation.qualityOfService = .UserInitiated

To get the most speed, you most likely you'll want to load the CKAsset images asynchronously , so wrap the player image loads in a background thread operation block.为了获得最大速度,您很可能希望异步加载CKAsset图像,因此将播放器图像加载包装在后台线程操作块中。

Here is and example from my code that fetches photo's that you can extrapolate from:这是我的代码中的示例,它获取您可以从中推断出的照片:

var backgroundQueue = NSOperationQueue()

func fetchPhoto(completion:(photo: UIImage!) -> ()) {

  backgroundQueue.addOperationWithBlock() {
    let image = self.record["Photo"] as? CKAsset
    if let ckAsset = image {
      let url = ckAsset.fileURL
      if let imageData = NSData(contentsOfURL:url) {
        self.photo = UIImage(data: imageData)
      }
    }

    completion(photo: self.photo)
  }
 }

Then for the table load in cellForRowAtIndexPath you can load the image via the completion block, but you MUST put all UI on the main thread or you will experience random crashes, as in:然后对于cellForRowAtIndexPath的表格加载,您可以通过完成块加载图像,但您必须将所有 UI 放在主线程上,否则您将遇到随机崩溃,如下所示:

object.fetchPhoto {
  image in
   dispatch_async(dispatch_get_main_queue()) {
    cell.photo.image = image
   }
}

I also encountered the same problem.我也遇到了同样的问题。

4 records took 12 seconds to load. 4 条记录需要 12 秒才能加载。

Problem问题

Performance issues come from CKAsset性能问题来自CKAsset

Solution解决方案

My solution is remove CKAsset (avatar/image).我的解决方案是删除CKAsset (头像/图像)。 Store avatar/image in icloud storage (Usage is a little complicated).将头像/图像存储icloud 存储中(使用有点复杂)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM