Learn how to create queries and pass them in to CloudKit's convenience API to get the records you need.
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3413-introduction-to-cloudkit/lessons/5
Learn how to create queries and pass them in to CloudKit's convenience API to get the records you need.
I’m on Xcode 7.2.1 (7C1002) and Deployment target: 9.2. I’m on CloudKit Part 5, Fetching Data. I’ve have the following error and would appreciate your assistance.
This is an older tutorial made using Swift 1.0, so there’s going to be errors using Swift 2. It’s been a long time since I’ve worked on this project.
That said, it looks like ckAsset.fileURL
is no longer an optional, so you don’t have to wrap it in a if-let block.
Here’s the code that should compile:
weak var weakCell = cell
backgroundQueue.addOperationWithBlock() {
let image = ckRecord.objectForKey("Photo") as! CKAsset!
if let ckAsset = image {
let imageData = NSData(contentsOfURL:ckAsset.fileURL)
let mainQueue = NSOperationQueue.mainQueue()
mainQueue.addOperationWithBlock() {
cell.photo.image = UIImage(data: imageData!)
}
}
}
If you need anything else, feel free to shoot me a message.
Thanks!
Hi Brian,
In your above example from the series there is a weak reference to a cell that is created to presumably stop a retain cycle, but it’s not being used. Should the addOperation() code for the mainQueue use it as below?
weakCell.photo.image = UIImage(data: imageData!)
And if so, can you explain how leaving the strong reference would cause a retain cycle?
Thanks,
Dan
I don’t get any records or error returned from my Query. I have:
struct Options
// these for cloud management
static var galleryContainer:CKContainer = CKContainer.default()
static var galleryDatabase:CKDatabase = galleryContainer.publicCloudDatabase
static var galleryData:[CKRecord]?
static var backgroundQueue:OperationQueue = OperationQueue()
}
let myQuery = CKQuery(recordType:"Gallery", predicate:NSPredicate(value:true))
Options.galleryDatabase.perform(myQuery, inZoneWith:nil) {
records, error in
if error != nil {
print(error?.localizedDescription)
} else {
print("no error in query")
for aGalleryRecord in records! {
self.galleryData!.append(aGalleryRecord as CKRecord)
print (" got record: \(aGalleryRecord)")
}
}
}
I specify iCloud.com.jaymanderson.gallery for my container.
I think I populated the container with one record of type “Gallery”.
What can I try next?
– jay
Could you update it to swift 3 there’s a lot of change since
It’s on our radar for sure!