Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between CKQueryOperation and Perform(Fetch...)

Tags:

ios

cloudkit

I'm new to working with CloudKit and database fetching and I've looked at the CKDataBaseOperation calls, so I'm trying to understand the real differences between adding an operation to a database and using "normal" function calls on that database if they both produce, more or less, the same results.

Why would adding an operation be more desirable over a function call and in what situations?

Thanks for helping me understand this. I'm trying to learn as much as I can about Swift.

like image 330
JamesH Avatar asked Feb 18 '26 11:02

JamesH


1 Answers

Overview:

In CloudKit most of the tasks have 2 ways of doing things:

  • Convenience APIs (functions with completion handlers)
  • Operations

1. Convenience APIs

Advantages:

  • As the name implies, they are convenient to use

Disadvantage:

  • Usually requires more server requests.
  • Can't build dependencies

2. Operations:

Advantages:

  • More configurable and more options.
  • Requires lesser server requests (Better for your server request quota)
  • It is built using Operation, so you get all the capabilities of Operation like dependencies (you will need them in a real app)

Disadvantages:

  • It is not so convenient to use, you need to create the operation. It takes a little more time to code but well worth it.

Example 1 (Fetch):

  • If you use CKDatabase.fetch, you would need to specify the record IDs that you want to fetch.
  • If you use CKQueryOperation, you can query based on field values.

Example 2 (Save / Update):

  • If you use CKDatabase.save, you can save 1 record with every function call. Each function call would result in a separate server request. If you want to save 200 records, you would have to run it in a loop and would make 200 server requests which is not very efficient. CloudKit also has a limit on the number of server requests you can make per second. This way you would exhaust your quota very quickly.
  • If you use CKModifyRecordsOperation, you can save 200 records all at once*, by passing it as an array. So you would be making far lesser server requests.

*Note: The server imposes a limit on the number of records it can save in 1 request but it is definitely better than creating a separate request to save each record.

Reference:

  • https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/Introduction/Introduction.html#//apple_ref/doc/uid/TP40014987-CH1-SW1
  • Watch WWDC CloudKit videos
  • Might help to learn and watch WWDC videos about Operation (earlier used to be referred as NSOperation)
like image 119
user1046037 Avatar answered Feb 20 '26 02:02

user1046037



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!