Fetching CKAsset Image From CloudKit is Very Slow

前端 未结 2 629
面向向阳花
面向向阳花 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:19

    There seems to be something slowing down your main thread which introduces a delay in executing the capture block of your dispatch_async call. Is it possible that your code calls this record fetching function multiple times in parallel ? This would cause the NSData(contentsOfURL: asset.fileURL) processing to hog the main thread and introduce cumulative delays.

    In any case, if only as a good practice, loading the image with NSData should be performed in the background and not on the main thread.

    0 讨论(0)
  • 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)
    }
    
    0 讨论(0)
提交回复
热议问题