Is it possible to fix slow CKAsset loading on Cloudkit?

I am trying to create a specialised photo sharing social network using CloudKit alone. The app will never need to be non iOS, so on the surface Cloudkit has seemed like a perfect backend. However, I've found at random but semi frequent times, the loading of CKAssets from records can be very slow.

For example, at normal times, all of the assets can load in a second or less. However at other times, it can take from 5 to 15 seconds. This is on a public database using a fetch operation where I already have the record IDs. Here is my code that performs that:

    
    mainDatabase.fetch(withRecordIDs: recordIDS, desiredKeys: ["image"]) { results in
        
        var recordsToReturn: [CKRecord] = []
        
        switch results {
        case .success(let success):
            
            for (_, result) in success {
                switch result {
                case .success(let record):
                    // If the result is success, append the record to the successfulRecords array
                    recordsToReturn.append(record)
                    break
                case .failure(_):
                    break
                }
            }
            
            
            completion(.success(recordsToReturn))

        case .failure(let failure):
            completion(.failure(failure))
        }
    }
}

Of note, I have also tried using a database operation at set the qualityOfService to userInitiated, which has had the same noticeable load times.

I have also tested this on different devices, on different networks (both cellular & different WiFi connections) and found also the same noticeable load times at random occurrences.

Therefore, what I am trying to find out:

  • Is this an expected behaviour from using CloudKit?
  • Is there a much better way to load CKAssets that would drastically help this load time issue?
  • Is CloudKit even a viable option for this kind of app, or is it not designed for this type of app?
  • What alternative approaches could be taken? (Eg. store assets in AWS...)

Would truly appreciate any feedback & guidance on this issue.

Replies

ooh ah, you ask a lot of important questions…

Is this an expected behavior from using CloudKit?

To some degree, yes, as it is specifically designed for backup and sync, and only incidentally does web. Speed is not a feature. That said, you should definitely be using qualityOfService so the system can optimize results. For instance, you might set it high to retrieve thumbnails, and set it low for background uploads.

Is CloudKit even a viable option for this kind of app, or is it not designed for this type of app?

I think it is viable, though on the public database you have to give very serious consideration to moderation, as well as the limits on public space as it related to the number of users of the app.

What alternative approaches could be taken? (Eg. store assets in AWS...)

you could go with another cloud service, but then you'd have to consider the costs.

Also, regardless of your backend, think thru how to handle large uploads and downloads in the background. For CloudKit, this means leveraging long-lived operations. See CloudCore for an example https://github.com/deeje/CloudCore/blob/master/Source/Classes/Caching/CloudCoreCacheManager.swift

Thank you for your comments @deeje . I'm curious about your mention regarding to "serious consideration to moderation". Would anything be different than it would when moderating on any other backend using a public database? I.e Firebase, AWS etc...?