Fetching CKAsset Image From CloudKit is Very Slow

前端 未结 2 628
面向向阳花
面向向阳花 2021-02-05 19:42

I am using CloudKit as a server backend for my iOS application. I\'m using it to house some relatively static data along with a handful of images(CKAsset). I ran into a problem

2条回答
  •  死守一世寂寞
    2021-02-05 20:23

    I am using CKQueryOperation. I found that once I added the following line to my code that downloading CKAssets sped up by about a factor of 5-10x.

        queryOperation.qualityOfService = .UserInteractive
    

    Here is my full code:

    func getReportPhotos(report:Report, completionHandler: (report:Report?, error:NSError?) -> ()) {
        let photo : Photo = report.photos![0] as! Photo
        let predicate : NSPredicate = NSPredicate(format: "recordID = %@", CKRecordID(recordName: photo.identifier!))
        let query : CKQuery = CKQuery(recordType: "Photo", predicate: predicate)
        let queryOperation : CKQueryOperation = CKQueryOperation()
        queryOperation.query = query
        queryOperation.resultsLimit = numberOfReportsPerQuery        
        queryOperation.qualityOfService = .UserInteractive
        queryOperation.recordFetchedBlock = { record in
            photo.date = record.objectForKey("date") as? NSDate
            photo.fileType = record.objectForKey("fileType") as? String
            let asset : CKAsset? = record.objectForKey("image") as? CKAsset
            if asset != nil {
                let photoData : NSData? = NSData(contentsOfURL:asset!.fileURL)
                let photo : Photo = report.photos![0] as! Photo
                photo.image = UIImage(data:photoData!)
            }
    
        }
        queryOperation.queryCompletionBlock = { queryCursor, error in
            dispatch_async(dispatch_get_main_queue(), {
                completionHandler(report: report, error: error)
            })
        }
        publicDatabase?.addOperation(queryOperation)
    }
    

提交回复
热议问题