Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • a Schedule object, which has a one-to-many relationship to ScheduleItem objects called "scheduleItems"
  • a ScheduleItem object has a dayOfWeek field and a one-to-one relationship with a Recipe object called "recipe"
  • a Recipe object has some information, including its name and ingredients (more relationships)

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.

like image 464
UberJason Avatar asked Oct 18 '25 21:10

UberJason


1 Answers

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

like image 52
Edwin Vermeer Avatar answered Oct 21 '25 13:10

Edwin Vermeer