Best way to query references in CloudKit?

China☆狼群 提交于 2019-12-04 07:07:44

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.

CloudKit is not supposed to be used as model layer, it is just connectivity framework, which provides you with database. You can use CoreData as model layer to solve your task. Create album entity, which have one to many relationship to photo entity, which has one to one relationship to album.

Then retrieve all albums first and map each record to corresponding Album entity in CoreData. Then get all photos, map them to Photo entity in CoreData and for each photo match corresponding Album entity in CoreData.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!