© Bruce Wade 2016

Bruce Wade, OS X App Development with CloudKit and Swift, 10.1007/978-1-4842-1880-8_5

5. Creating Test Data with CloudKit Dashboard

Bruce Wade

(1)Suite No. 1408, North Vancouver, British Columbia, Canada

In this chapter we will take information about the data we created in chapter 3 and create record types using the CloudKit Dashboard. Next, we will use the Dashboard to create some test data that we will add to our prototype in the next chapter.

Setting Up Our Project for CloudKit

In the previous chapter we took a look at CloudKit. However, when using the Dashboard our app was not listed in the container list. This is because we have not yet enabled CloudKit for our app. Let’s do that now.

First, you must have a developer account, because only apps distributed to the App Store can use CloudKit, and you need a developer account to upload to the App Store.

Next, as was already emphasized, you cannot change or delete your container once it is created, so before following the steps to enable CloudKit, make sure your bundle identifier is set to a value you want to keep. Then click on the Capabilities tab and toggle the On/Off switch for iCloud (see Figure 5-1).

A385012_1_En_5_Fig1_HTML.jpg
Figure 5-1. Turning on iCloud

You are going to want to enable key-value storage and CloudKit. Once you click CloudKit, your container will be created, so be sure to click the “Key-value storage” checkbox first. Now open CloudKit Dashboard by clicking the CloudKit Dashboard button, and you will see your app and container are ready for you to select and start adding data to.

Goals of Test Data

The primary goal of using test data before doing any programming is to help us quickly think about what data we want to display and make available to the user, and what public data we want to control ourselves without allowing the general users to create, update, or delete it. Finally, probably the most important aspect is to give our application some real data to work with so we can test what happens when the network is slow, when there is no Internet connection, or when we change the data in a different place then in our app. (Do we get push notifications? Does our app automatically update to show the new data?)

Creating the Parks Record Type

In the CloudKit Dashboard, make sure your app is selected from the top drop-down, and then select “Record Types” under Schema. You should only see the Users record type. In the detail pane click on the + icon to create a new record type, then name it Parks. Figure 5-2 shows the completed Parks record type.

A385012_1_En_5_Fig2_HTML.jpg
Figure 5-2. Complete Parks record type

Next, we need to add the following fields to hold the data:

  • name as a String with indexes for Sort, Query, and Search.

  • location as a String (this can also use the Location type, which we might add later) with no indexes

  • overview as String with no indexes

  • allowsOffleash as an Int(64) with all the indexes

  • hasFreshWater as an Int(64) with all the indexes

  • isFenced as an Int(64) with all the indexes

  • thumbnail as an Asset, which has no index options

We are using Int (64) for our Boolean fields because that is our only option with CloudKit; a value of 0 will mean the park doesn’t have the feature and 1 will mean it does. Finally, click the Save button to save your changes. We will be using the thumbnail later as an optimization step.

Creating the ParkImages Record Type

Now let’s create another record type called Park Images. We will use this record type for each of our page images and use a back reference to the Park record they belong to. Create the type with the following fields (Figure 5-3):

  • park as type Reference and index for Query. This will be used to link the parent park.

  • thumbnail as an Asset type; this will be a smaller, scaled-down version of the main image used for optimization purposes

  • image as an Asset type that will hold the full image

A385012_1_En_5_Fig3_HTML.jpg
Figure 5-3. Complete ParkImages record type

Security Role

Let’s create a security role so only we can create a park. We will, however, allow others to add images to the park. Click on Security Roles, then create a ParkManager role, with a record type called Parks that has create, read, and write permissions. See Figure 5-4.

A385012_1_En_5_Fig4_HTML.jpg
Figure 5-4. ParkManager security role

We will assign this role to our user at a later step once we integrate CloudKit into our app, as we currently have no users to assign the role to.

Create Parks Test Data

Now that we have our record Types set up, we are going to create some sample data. I recommend that you search for parks in your area using Google; otherwise, local-based searching within the app won’t work as expected. This will also get you into the habit of using real data and force you to think about what kind of data will be entered into the app.

We will create all of our test data in the Default Zone under public data. From the drop-down in the middle column, select “Parks.” We’ll start by creating some parks and then add images to the individual park.

First, enter the details from the following five parks. Either drag and drop your image into the Thumbnail field or click on the Upload button. This will be the featured thumbnail for the park in the Parks List.

1. name: Panorama Park
    allowsOffleash: 1
    hasFreshWater: 1
    isFenced: 0
    location: North Vancouver
    overview: Located in deep cove area.
    thumbnail: thumbnail.jpg (located in the TestParksData folder)
2. name: Kings Mill Park
    allowsOffleash: 1
    hasFreshWater: 1
    isFenced: 0
    location: North Vancouver  
     overview: Good relaxing park to take your dogs to play.
     thumbnail: thumbnail.jpg (located in the TestParksData folder)
3. name: Deep Cove Park
    allowsOffleash: 0
    hasFreshWater: 0
    isFenced: 0
    location: North Vancouver
    overview: This park is located in the beautiful deep cove area.
    thumbnail: thumbnail.jpg (located in the TestParksData folder)
4. name: Bridgeman Park
    allowsOffleash: 1
    hasFreshWater: 1
    isFenced: 0
    location: North Vancouver
    overview: Provides a large park and walking trails where your dog can run around offleash. There is a stream with fresh moving water dogs can drink out of.
    thumbnail: thumbnail.jpg (located in the TestParksData folder)
5. name: Ambleside Dog Park
    allowsOffleash: 1
    hasFreshWater: 1
    isFenced: 0
    location: West Vancouver
    overview: Located off the ocean of Ambleside beach in West Vancouver.
    thumbnail: thumbnail.jpg (located in the TestParksData folder)

Click Save after each entry and your test park will be created (Figure 5-5). Repeat the process to create the other parks.

A385012_1_En_5_Fig5_HTML.jpg
Figure 5-5. Sample park populated
Note

You should scale the featured thumbnail to 48 x 48 to optimize bandwidth.

Create ParkImages Test Data

For each park image contained within a park entry, you are also going to want to make a 90 x 90 pixel thumbnail version. Creating the thumbnails is an optimization, as there is no reason to download the full-size images if the user is only scrolling through a bunch of small images.

For each image you add to a park, you must provide the park’s record name. You can find this by first finding the park you want to add an image to in the default data and copying its record name (Figure 5-6). Next, click the checkbox for “DeleteSelf,” which will ensure that if the park record is deleted all images related to it will also be deleted.

A385012_1_En_5_Fig6_HTML.jpg
Figure 5-6. Selected record name

Next, change the middle column’s drop-down selection to “ParkImages” and click the + to create a new record. You want to first focus on the Park field that is a Reference type. Paste the park record name you copied into the Park field textbox. At this point you will notice that all images show as No Name. (Figure 5-7). You can either leave it or add a Name field to the ParkImages record type and use it to name your images. For this book we will leave it as it is, though we may change this later.

A385012_1_En_5_Fig7_HTML.jpg
Figure 5-7. ParkImages record type with a Parks record type name as a reference

If you are following along using the test data provided with the book, within the Images folder for each park you will see a file following the naming convention of parkname#, and for the thumbnail that name is appended with 90x90. Drag the image without the 90x90 to the Image field, and drag the matching thumbnail to the Thumbnail field. Make sure to add multiple images for each park before moving on to the next chapter.

Conclusion

This chapter has covered how the CloudKit Dashboard makes it very easy to create test data for your app. In the next chapter, we will upgrade our app so as to display this test data.

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

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