Understanding your CloudKit database

In order to efficiently use CloudKit to store data, you must understand how it stores data and what terminology is used for the different components that are used throughout your database. Every CloudKit container comes with a couple of different databases:

  • A private database for each user
  • A shared database for each user
  • A public database that's shared for all users

It's important that you use the correct database whenever you write data to CloudKit, you wouldn't want to accidentally store some sensitive user information in the public database because that could potentially expose the information to all users of your app. You should only use the public database for data that is suited for all users of an application.

For instance, if you're building a news app, it would make sense to store all the news and possibly comments on news articles in the public database for your container. However, saving a user's to-do list should be stored in the private database because no other users have any business with another user's to-do list.

One exception to this is when a user shares their list or individual items with another user. This shared data can be stored in the shared database where only users that have explicitly gained access to the records are allowed to access them.

Now that you know about the different types of databases, let's have a look at the data page itself to see what kind of options you have to manage data there.

The leftmost tab in the data manager is the Zones tab. You can use zones to logically group certain parts of your database together. For instance, you could create zones for data that originated from different origins. The private and public databases come with a default zone. You cannot create custom zones for the public database so all public data is automatically stored in the default zone. For the private and shared databases, you can create custom zones.

Note that the zones page has the option to Fetch zone changes since... This is a performance optimization that makes sure apps can load a minimal amount of data whenever they want to synchronize with the data from CloudKit. If you want to add a custom zone, you can do so by clicking on the Create new Zone... button. While you're there, create a new zone for the private database and name it moviesZone. This is where you'll store the data from the MustC app later.

The second tab, Records, is more interesting. On this tab, you can perform queries on the data that is stored in your databases and zones. This allows you to explore your data and make sure that everything is saved as you would expect. For privacy reasons, you can only see the data for your own private and shared database and the public database. You can never access a user's private database.

The records tab provides a window into your CloudKit database. Note that the records page also has a Changes tab. You can use this tab to retrieve changes to all records in the selected zone. Note that you can't do this for the default zone, so if you want to use this functionality in your app, you will need to create a custom zone on the Zones tab.

The third tab is the Record Types tab. This is where you define the objects you want to store in your CloudKit database and what properties they have. Note that record types are not connected to a zone. This means that you can write a certain record to any zone you'd like. Keep in mind though that records from different zones cannot reference each other. So if you consider the MustC app, a family member and their favorite movies would have to exist in the same zone. If this isn't the case, the objects are not allowed to reference each other in CloudKit.

To prepare for storing data later, you should create your record types now. Click the Create New Type button and name it FamilyMember. Add a field to it by clicking the Add field button and call the field name. The type should be String. Add another field and call it movies, give it a type of Reference (list). The following screenshot shows what your fields should look like:

Click the Create Record Type button to store your record type and create a second record type for the movie records. Use Movie as the name for your record type and give it three properties, named title, which should be a String, rating (Double), and a remoteId (Int64). Make sure to save this record type as well.

After you save a record type, CloudKit automatically adds some extra metadata properties to it. These properties are used internally by CloudKit to figure out what objects changed and when. You can't remove or modify these properties because CloudKit needs them.

The fourth tab in the dashboard is the Indexes tab. This tab is used to specify which properties of a record will be optimized for querying. If you don't specify any indexes, you won't be able to easily fetch all records from your CloudKit database. Typically you will want to at least add an index for the recordName property. To do this, select your record type and click the Add Index button. Select the recordName property in the first dropdown and QUERYABLE from the second. Do this for both record types you created earlier.

The fifth tab is Subscriptions. This is where you can see the subscriptions that exist for a particular database. You can only look at the subscriptions for your own private and shared database, and the public database. You can use this tab to check whether you are correctly subscribing apps to database updates.

The next tab is Subscription types. There isn't much to do here other than explore the automatically-generated subscription types and check whether they match what you expect to find there.

The last tab is the Security Roles tab. This tab allows you to manage what permissions users have for record types in the public database. This allows you to limit the actions that a certain user can perform. For instance, you might want to allow all users to read news articles from the public database but restrict writing new articles to the database to your administrative users. You can manage all this in the security roles tab. The default values will suffice for the MustC app.

Let's go back to your container overview page to discuss the other available sections briefly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.121.55