简体   繁体   English

CKQueryOperation和Perform(Fetch ...)之间的区别

[英]Difference between CKQueryOperation and Perform(Fetch…)

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. 我刚开始使用CloudKit和数据库获取,我查看了CKDataBaseOperation调用,所以我试图理解在数据库中添加操作和在数据库上使用“普通”函数调用之间的真正区别两者都产生或多或少相同的结果。

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. 我正在努力学习关于斯威夫特的事情。

Overview: 概述:

In CloudKit most of the tasks have 2 ways of doing things: CloudKit大多数任务都有两种处理方式:

  • Convenience APIs (functions with completion handlers) 便利API(具有完成处理程序的功能)
  • Operations 操作

1. Convenience APIs 1.便利API

Advantages: 好处:

  • As the name implies, they are convenient to use 顾名思义,它们使用起来很方便

Disadvantage: 坏处:

  • Usually requires more server requests. 通常需要更多服务器请求。
  • Can't build dependencies 无法构建依赖项

2. Operations: 2.运营:

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) 它是使用Operation构建的,因此您可以像依赖项一样获得Operation的所有功能(您将在真实的应用程序中使用它们)

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): 示例1(获取):

  • If you use CKDatabase.fetch , you would need to specify the record IDs that you want to fetch. 如果使用CKDatabase.fetch ,则需要指定要获取的记录ID。
  • If you use CKQueryOperation , you can query based on field values. 如果使用CKQueryOperation ,则可以基于字段值进行查询。

Example 2 (Save / Update): 示例2(保存/更新):

  • If you use CKDatabase.save , you can save 1 record with every function call. 如果使用CKDatabase.save ,则可以在每次函数调用时保存1条记录。 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. 如果要保存200条记录,则必须在循环中运行它,并且会产生200个服务器请求,这些请求效率不高。 CloudKit also has a limit on the number of server requests you can make per second. CloudKit还限制了您每秒可以进行的服务器请求数。 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. 如果使用CKModifyRecordsOperation ,则可以一次性保存200条记录*,方法是将其作为数组传递。 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. *注意:服务器对可以在1个请求中保存的记录数量施加限制,但它肯定比创建保存每个记录的单独请求更好。

Reference: 参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用CloudKit的CKQueryOperation从iCloud中获取数据 - Can't fetch data from iCloud using CloudKit's CKQueryOperation 执行segue与标识符和准备segue之间的区别 - Difference between perform segue with identifier and prepare for segue iOS:“后台抓取”和“后台刷新”有什么区别 - iOS: What is the difference between “background fetch” and “background refresh” 获取请求(模型模板)与核心数据中的提取属性之间的差异 - Difference between Fetch Requests (model templates) and Fetched Properties in Core Data CKQueryOperation queryCompletionBlock 未调用 - CKQueryOperation queryCompletionBlock not called CloudKit - 具有依赖性的CKQueryOperation - CloudKit - CKQueryOperation with dependency 直接执行方法与在dispatch_async(dispatch_get_main_queue(),^ {})块中有什么区别 - What is the difference between perform a method directly and in the block of dispatch_async(dispatch_get_main_queue(), ^{}) CKQueryOperation resultLimit max 和请求数 - CKQueryOperation resultLimit max and request count CKQueryOperation queryCompletionBlock 返回零 cursor - CKQueryOperation queryCompletionBlock return a nil cursor 在不冻结UI的情况下执行提取 - Perform fetch without freezing the UI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM