Best way to query references in CloudKit?

cloudkit, ios

Solution

For each of the album object anAlbum, you would form your predicate and query the Photo record type like so:

let predicate = NSPredicate(format: "album == %@", anAlbum)
let query = CKQuery(recordType: "Photo", predicate: predicate)

In the completionHandler, you can get a count of the result array, which would be the number of Photos belonging to anAlbum.

Problem

Say I have 2 entities in my CloudKit database: ``` - Album - (NSString) title - Photo - (NSString) name - (CKRefrence) album ``` Photos have a CKReference to an Album. This means that 1 album can have many photos (as expected). I have a screen where I want to display all albums, and how many photos are in each album. What is the best way to query for this? If I query for Albums right now, each album knows nothing about its photos.

Original source