I'm using CloudKit in my app as a way to persist data remotely. One of my records has an CKAsset property that holds an image. When I'm fetching the records, I realized that it's taking so much time to finish the query. After multiple testings, I concluded that when you query records, CloutKit downloads the entire Asset file with the record object. Hence, when you obtain the Asset from the record object and request it's fileURL, it gives you a local file path URL and not an HTTP kind of URL. This is as issue to me because you have to let the user wait so much time for the entire records to be downloaded (with their associated images) before the query ends. Coming from a Parse background, Parse used to give you the HTTP URL and the query is fast enough to load the UI with the objects while loading the images async. My question is, how can I achieve what Parse does? I need to restrict the queries from downloading the Assets data and obtain a link to load the images asynchronously.
5 Answers
You can use CloudKit JavaScript for accessing url of asset. Here is an example;
(Used Alamofire and SwiftyJSON)
func testRecordRequest() -> Request {
let urlString = "https://api.apple-cloudkit.com/database/1/" + Constants.container + "/development/public/records/query?ckAPIToken=" + Constants.cloudKitAPIToken
let query = ["recordType": "TestRecord"]
return Alamofire.request(.POST, urlString, parameters: ["query": query], encoding: .JSON, headers: nil)
}
JSON response contains a "downloadURL" for the asset.
"downloadURL": "https://cvws.icloud-content.com/B/.../${f}?o=AmVtU..."
"${f}" seems like a variable so change it to anything you like.
let downloadURLString = json["fields"][FieldNames.image]["value"]["downloadURL"].stringValue
let recordName = json["recordName"].stringValue
let fileName = recordName + ".jpg"
let imageURLString = downloadURLString.stringByReplacingOccurrencesOfString("${f}", withString: fileName)
And now we can use this urlString to create a NSURL and use it with any image&cache solutions such as Kingfisher, HanekeSwift etc. (You may also want to save image type png/jpg)

- 109
- 1
- 5
-
-
1@drdrdrdr [Keys.image] -> field name of the image on the record type. You can check it on CloudKit dashboard. It was confusing, just changed it. Thanks – anilgoktas Aug 31 '16 at 08:16
-
@anilgoktas This request returns `"serverErrorCode = "AUTHENTICATION_REQUIRED";"` with the redirectURL to the login webpage. How did you overcome this? – Vadim Popov Oct 26 '16 at 08:39
-
I am trying @anilgoktas approach as I need the URL for a video I have allocated in iCloud. However, the post request returns: `"BAD_REQUEST","reason":"BadRequestException: missing required query field"`. I am using `recordType` for quering. Any ideas? – Victor Nov 12 '16 at 01:26
-
Hey @anilgoktas, how would this function be without SwiftyJSON? Without Alamofire I can do it this way: https://stackoverflow.com/questions/26364914/http-request-in-swift-with-post-method – drv Jan 05 '18 at 20:25
-
1@drdrdrdr you can use `let json = JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any]` from the [answer](https://stackoverflow.com/a/41082877/3706378) you sent. – anilgoktas Jan 07 '18 at 12:11
-
@drv Check out my answer [below](https://stackoverflow.com/a/69789922/12826591). I was able to get the URL and other metadata through that method – Yush Raj Kapoor Oct 31 '21 at 19:34
Make two separate calls for the same record. The first call should fetch all the NON-asset fields you want, and then second request should fetch the required assets.
Something like:
let dataQueryOperation = CKQueryOperation(query: CKQuery(predicate: myPredicate)
dataQueryOperation.desiredKeys = ["name", "age"] // etc
database.addOperation(dataQueryOperation)
let imageQueryOperation = CKQueryOperation(query: CKQuery(predicate: myPredicate)
imageQueryOperation.desiredKeys = ["images"]
database.addOperation(imageQueryOperation)
If need, refactor this into a method so you can easily make a new CKQueryOperation for every asset-containing field.
Happy hunting.

- 487
- 6
- 18
Like others have said you cannot get the url(web) of the CKAsset. So your best options are 1. Use a fetch operation with progress per individual UIImageView. I have built a custom one that shows a progress to the user. Cache is not included but you can make a class and adopt NSCoding and save the entire record to cache directory. Here you can see a fetch that i have a completion on to send the asset back to where i call it from to combine it with my other data.
// only get the asset in this fetch. we have everything else
let operation = CKFetchRecordsOperation(recordIDs: [myRecordID])
operation.desiredKeys = ["GameTurnImage"]
operation.perRecordProgressBlock = {
record,progress in
dispatch_async(dispatch_get_main_queue(), {
self.progressIndicatorView.progress = CGFloat(progress)
})
}
operation.perRecordCompletionBlock = {
record,recordID,error in
if let _ = record{
let asset = record!.valueForKey("GameTurnImage") as? CKAsset
if let _ = asset{
let url = asset!.fileURL
let imageData = NSData(contentsOfFile: url.path!)!
dispatch_async(dispatch_get_main_queue(), {
self.image = UIImage(data: imageData)
self.progressIndicatorView.reveal()
})
completion(asset!)
}
}
}
CKContainer.defaultContainer().publicCloudDatabase.addOperation(operation)
The other option is to store images on an AWS server or something comparable and then you can use something like SDWebImage to do all of the cache or heavy lifting and save a string in the CKRecord to the image.
I have asked several people about a CKAsset feature to expose a url. I don't know about the JS Api for CloudKit but there might be a way to do it with this but i will let others commment on that.

- 4,173
- 2
- 19
- 24
Using the Post method from @anilgoktas, we can also get the download URL without using Alamofire and SwiftyJSON, although it may be a little more complicated and not as neat.
func Request() {
let container = "INSERT CONTAINER NAME"
let cloudKitAPIToken = "INSERT API TOKEN"
let urlString = "https://api.apple-cloudkit.com/database/1/" + container + "/development/public/records/query?ckAPIToken=" + cloudKitAPIToken
let query = ["recordType": "testRecord"]
//create the url with URL
let url = URL(string: urlString)! //change the url
//create the session object
let session = URLSession.shared
//now create the URLRequest object using the url object
var request = URLRequest(url: url)
request.httpMethod = "POST" //set http method as POST
do {
request.httpBody = try JSONSerialization.data(withJSONObject: ["query": query], options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body
} catch let error {
print(error.localizedDescription)
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
//create dataTask using the session object to send data to the server
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil else {
return
}
guard let data = data else {
return
}
do {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
DispatchQueue.main.async {
for i in json.values {
if let sub = i as? NSArray {
for j in sub {
if let dict = j as? NSDictionary, let subDict = dict["fields"] as? NSDictionary, let titleDict = subDict["title"] as? [String:String], let title = titleDict["value"], let videoDict = subDict["video"] as? NSDictionary, let vidVal = videoDict["value"] as? NSDictionary, let url = vidVal["downloadURL"] as? String {
let downloadURL = url.replacingOccurrences(of: "${f}", with: "\(title).jpg")
print(title)
print(downloadURL)
}
}
}
}
// handle json...
}
}
} catch let error {
print(error.localizedDescription)
}
})
task.resume()
}

- 51
- 7
Here is my approach.
Let's say I have Record Type : Books
BookID (auto-id or your unique id)
BookName string
BookImage CKAsset
BookURL string
Incase I use CKAssest I store in BookURL : "Asset:\BookID.png " Incase I used external server to store the images, I use normal URL in BookURL "http://myexternalServer\images\BookID.png"
Queries :
with desiredKeys I query all fields without BookImage(CKAsset) field.
if BookURL is empty , there is no image for the book.
if BookURL start with "Asset:\" I query for BookImage from cloudkit
if BookURL is normal URL I download the image from external server (NSUrlSession)
This way , any time I can change and decide how to store image, on cloudkit(CKAssets) or on external server (http downloads)

- 2,259
- 2
- 34
- 55