Search code examples
iosiphoneswifticloudcloudkit

How to fetch records from a specific record zone


I'm building an app that allows users to manage their products in the cloud. To do this, I have a Product Record type in iCloud. I use this record type for two purposes:

  • To manage the products I have available, all stored in a dedicated Products record zone
  • To save products I have stored in checks from customers. I have a separate Checks record zone, in which I save checks or 'receipts' from transactions made with customers. I use the Product record type here too, in order to keep track of what products are being sold.

Now I have one problem: When I try to fetch all products for my product manager, it also fetches the products from the Checks record zone - the zone containing the transactions and the products they contain. I obviously need to fetch only the products from the Products Record Zone.

There doesn't seem to be much reference for this on the internet, so I figured I'd post a question here for, also for future reference.

Is there a way in CloudKit to fetch records from a specific record zone?


Solution

  • You could use a CKQueryOperation and set the zoneID like this:

        let operation = CKQueryOperation(query: query)
        operation.zoneID = CKRecordZoneID(zoneName: "name", ownerName: "me")