8

In my CloudKit setup I have a Person record type and each person has a Friend attribute which is a CKReference to another Person object.

I fetch all Person CKRecords. I list these people in a table and list their friends with them. Currently I just take the CKReference and individually fetch each friend using the recordID.

This is the only way I can see to do it, but what would be best practice? Particularly as I am fetching potentially a very large number of friends. It seems counterintuitive to fetch all these objects individually.

I was wondering if there was a method much like CoreData, where you can choose to fetch related objects in the main fetch? So for example, when I fetch a Person it automatically fetched the friends too.

halfer
  • 19,824
  • 17
  • 99
  • 186
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253

2 Answers2

5

You could use an 'in' predicate query to fetch multiple id's. You can define a predicate like this:

NSPredicate(format: "To_ID in %@", [recordIdMe, recordIdOther])!

In your case where you see the recordIdMe and recordIdOther you should have an array of valid CKReference objects.

In CloudKit there is no functionality to automatically fetch related records.

gohnjanotis
  • 6,513
  • 6
  • 37
  • 57
Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
2

Actually you should collect all the RecordIDs and add a CKFetchRecordsOperation to the relevant database object.

You get a back a dictionary to help you match the results with the original list.

Yariv Nissim
  • 13,273
  • 1
  • 38
  • 44