UberJason
UberJason

Reputation: 3163

CloudKit: fetch record and references in one query (like in Parse)

With the impending death of Parse, I'm re-writing an app using CloudKit. Suppose I'm making a recipe scheduling app. I want a given schedule to contain an ordered list of recipes, and recipes can be in multiple schedules. So I have:

Assume I have the Schedule object, and I want to get all of its ScheduleItems AND their associated Recipes in one single query. In Parse, I could set up this query:

PFQuery *query = [PFQuery queryWithClassName:@"Schedule"];
[query includeKey:@"scheduleItems.recipe"];

And that query would fetch all the scheduleItems along with all their recipes, allowing me to avoid having to perform multiple network requests.

Is there a way to do this in CloudKit? I see that I can use a CKFetchRecordsOperation to fetch multiple records at once given their recordIDs, but I wouldn't know the record IDs of the recipes until I had already fetched the scheduleItems, thus still necessitating a second network request.

Upvotes: 4

Views: 1782

Answers (2)

Rob
Rob

Reputation: 11733

There is a way in the coming version ('16): you can define parent relations and then fetching the parent records will result in the children being fetched as well. This is covered in the WWDC session 'What's New in CloudKit' (2016): https://developer.apple.com/videos/play/wwdc2016/226/

Correction, the parent reference introduced in '16 is not to make it possible to do a single fetch, but rather to automatically share all descendants, when using the new Sharing part of the API. So the answer is no there is still no way but when sharing records, it is a bit less onerous.

Upvotes: 2

Edwin Vermeer
Edwin Vermeer

Reputation: 13127

CloudKit is not a relational database. It's a key-value store. There is no functionality to query multiple recordType's in one query. There is also no functionality for aggregation queries. In your case since it's a one to one relationship you could limit it to 2 queries by adding a CKReference in your Recipe to the Schedule. So then if you have a schedule, you could do one query to get all related ScheduleItem's and an other query to get all related Recipe's

Upvotes: 3

Related Questions