Search code examples
ioscocoa-touchswiftcloudkit

Create a CloudKit record type programmatically


How do I programmatically create a record type in CloudKit, without doing it in the dashboard? For example: in the app that I am making, a user will create a group, let's call it "Cars". Then the user will add many different cars to that record type, like "Honda", "Toyota", and so on. How do I do this programmatically? I'm working in Swift.


Solution

  • I think you're confusing record types and record values. For your example you would want a Car record type, and then you would want a Manufacturer value in the record that is set to "Honda", "Toyota", etc.

    You could also design this by creating a Manufacturer record type, creating one record for each manufacturer, and having a reference from the Car record to its Manufacturer record.

    To create a new Car record just create a CKRecord object, set the properties you want, and save it to the server using CKDatabase.saveRecord().

    To answer the question in your title, record types are created for you automatically when you save a record to the development environment. You can not modify record types or schemas programmatically in the production environment.

    You can read more about the development and production environments in the CloudKit Quick Start Guide.