An operation used to retrieve records from CloudKit.
- iOS 8.0+
- macOS 10.10+
- UIKit for Mac 13.0+Beta
- tvOS 9.0+
- watchOS 3.0+
Use this operation to retrieve the entire contents of each record or only a subset of its contained values. As records become available, the operation object reports progress about the state of the operation to several different blocks, which you can use to process the results.
Fetching records is a common use of CloudKit, even if your app does not cache record IDs locally. For example, when you fetch a record related to the current record through a
CKRecord object, you use the ID in the reference to perform the fetch.
The blocks you assign to process the fetched records are executed serially on an internal queue managed by the fetch records operation. Your blocks must be capable of executing on a background thread, so any tasks that require access to the main thread must be redirected accordingly.
In addition to data records, a fetch records operation can fetch the current user record. The
fetch method returns a specially configured operation object that retrieves the current user record. That record is a standard
CKRecord object whose contents are empty initially. You can add data to the user record and save it as needed. Because a discoverable user record can be accessed by other users of the app, never store sensitive personal information such as passwords in the user record. If you must store sensitive information about a user, do so in a separate record that is accessible only to that user.
If you assign a completion block to the
completion property of the operation object, the completion block is called after the operation executes and returns its results to you. You can use a completion block to perform housekeeping chores related to the operation, but do not use it to process the results of the operation itself. Any completion block you specify should be prepared to handle the failure of the operation to complete its task, whether due to an error or an explicit cancellation.