4

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.

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer, that's a bummer. Which 1-to-1 relationship were you referring to? In my case, actually, Recipes could be associated with multiple ScheduleItems (and therefore, indirectly, multiple Schedules), but your idea would still work using Reference Lists and a CKQuery, right?
You mentioned this: a ScheduleItem object has a dayOfWeek field and a one-to-one relationship with a Recipe object called "recipe"
If both ScheduleItem and Recipe can have a CKReference to Schedule, then you could fire both queries at the same time. Your suggested solution would also work. first query ScheduleItem and then query Recipe with all the id's. What situation works best for you depends on your other requirements for these recordTypes.
Ah, sorry - a ScheduleItem is 1-to-1 to a Recipe, but the inverse relationship (conceptually) could be 1-to-many. But it should come down to the same thing, and I understand your solution. Thank you!
2

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.

2 Comments

Thanks for the reply - I remember watching that session, also getting excited when they mentioned "parent relations", and then getting disappointed when it wasn't what I wanted. :)
I was going to delete my answer since it's kind of a non-answer but then Edwin's is too. Ultimately threads like this one: stackoverflow.com/questions/24788415/… speak the truth: you are supposed to be using it simply as a message layer, but even messages sometimes have graphs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.