Search code examples
ioscloudkit

Some questions about CloudKit


record type: Country(CKRecord)
--------------------
name: String
code: String
--------------------

record type: User(CKRecord)
------------------------
name: String
country: CKReference (reference to country record>
-------------------------

I have 2 record types (Country and User) created by CloudKit Dashboard. I encountered some cloudkit issues.

currently! I query User and then using country reference to fetch country record. When I using cloudKit to get some records I have to do a lot of queries and fetches that waste a lot of time for waiting. so my issues are

1)How to get User record and Country record at the same time, If I query a record? example: query User Record
2)In some record that have a reference list. How to delete a CKReference in that reference list

Thanks a lot


Solution

  • In CloudKit there is no way to query 2 different recordTypes in 1 query. The only exception is when you already know the specific RecordId's. Then you could query for those ID's. So In your case, when you don't already know the User, you do have to do 2 queries to get the country record.

    You could limit the number of fetches since you probably have a limited number of countries. At app startup just query all the countries and save them somewhere in memory, then when you do query for a user, you can get the country from memory. If you suspect that your country data will change frequently, then create a subscription so that you get a notification to update your data in memory.

    If you have a reference list in your record, then query that record, remove the item from the list and save that record again.