在CloudKit中查询引用的最佳方法? [英] Best way to query references in CloudKit?
问题描述
假设我的CloudKit数据库中有2个实体:
Say I have 2 entities in my CloudKit database:
- Album
- (NSString) title
- Photo
- (NSString) name
- (CKRefrence) album
照片的相册参考为CK。这意味着1张相册可以包含许多照片(按预期方式)。
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.
推荐答案
对于每个相册对象anAlbum,您将形成谓词并查询Photo记录类型,如下所示:
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)
在completionHandler中,您可以获取结果数组的计数,这就是属于相册的照片数量。
In the completionHandler, you can get a count of the result array, which would be the number of Photos belonging to anAlbum.
这篇关于在CloudKit中查询引用的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!