Skip to content

CloudKit creating records.

Roman Babenko edited this page Sep 12, 2020 · 1 revision

ISN_CKRecord is a dictionary of key-value pairs that you use to fetch and save your app's data. Records are the fundamental objects you use to manage data in CloudKit. You may define any number of record types for your app, with each record type corresponding to a different type of information you need. Within a given record type, you then define one or more fields, each of which has a name and a data value. Records can contain simple data types such as strings and numbers or more complex types such as NSData or ISN_CKAsset.

An important step in using CloudKit is defining the record types your app supports. Each new record object contains no keys or values initially. During development, you can add new keys and values at any time. The first time you set a value for a key and save the record, the server associates that type with the key for all records of the same type. (The ISN_CKRecord class does not enforce these type constraints or do any local validation of a record’s contents; those constraints are enforced by the server when you save records.)

Although records act like dictionaries, there are still limitations to the types of values you can assign to keys. The following are the object types that the CKRecord class supports. Attempting to specify objects of any other type is a programmer error and will fail. Fields of all types are searchable unless otherwise noted.

Record field types.

The following data types are supported in ISN_CKRecord fields.

  • String - Store relatively small amounts of text. Although strings themselves can be any length, use an ISN_CKAsset to store large amounts of text.
  • Number - Store any numerical information, including integers numbers.
  • Data - Store arbitrary bytes of data. A typical use for data objects is to map the bytes that they contain to a struct. Do not use data objects for storing large binary data files; use an ISN_CKAsset instead. Data fields are not searchable.
  • Date - Store day and time information in an accessible form.
  • ISN_CKAsset - Associate a disk-based file with the record. Assets are intimately tied to records but are managed separately. For more information about using assets, see CKAsset. Simply put this is just a string URL to the file in the system.

Creating recordID.

In order to create a record, you first need to create ISN_CKRecordID for it. It is a unique ID of the record. The ID of a record never changes over the lifetime of that record.

When you save a new record object to the server, the server validates the uniqueness of the record but reports an error only if the save policy calls for it. Specifically, it reports an error when the save policy is set to CKRecordSaveIfServerRecordUnchanged, which is the default. For other save policies, the server overwrites the contents of the existing record accordingly. You can create new ISN_CKRecordID like this

var recordID = new ISN_CKRecordID("uniqeu name");

here unique name is a unique RecordName. In the future is you know only this RecordName you can create new instances of the same recordID that you can fetch or delete record.

Creating record

When you have recordID you can create a record for this id, like this

var record = new ISN_CKRecord("TestType", recordID);

here you create new ISN_CKRecord with RecordType - TestType and with recordID that you should create before it. RecordType can be used in the CloudKit dashboard for searching for records by their type. When you created your record you can add as many fields to it as you want, you can do it by using ISN_CKRecord.AddField(string key, ISN_CKAsset value) here instead of ISN_CKAsseet type as value type for this field you can use any other 4 types that were described above.

Get data from the record

When you fetched some record from iCloud, you can also get all fields that this record contains. For this purpose ISN_CKRecord contains methods for getting field values by their keys - for example this ISN_CKRecord.GetDateTimeValue(string key) method will return DateTime value from field for given key if we don't have field for given key it will return DateTime.Now.

About

Foundation

AV Foundation

App Tracking Transparency

Game Kit

Store Kit

UI Kit

Social

Replay Kit

Contacts

AVKit

Photos

App Delegate

User Notifications

MediaPlayer

Core Location

AdSupport

EventKit

CloudKit

Authentication Services

XCode

Knowledge Base

Clone this wiki locally