Thursday, June 4, 2015

A Simple Swift iOS App from Start to Finish - Creating the Data Model

Introduction

This is part of a series of articles describing the creation of a simple iOS app. A complete list of the articles is included in the first part A Simple Swift iOS App from Start to Finish - Introduction.

I have set up a web site for the finished app at http://DaysWithoutThings.com and you can download it free directly from the app store:




In this article I am going to create the Data model for my app. Core Data provides an interface that is very similar to a relational database except it defines data in terms of entities rather than tables. My app is going to keep records for the things it is tracking and the notifications it will attach to those things. To implement this I am going to create two entities which I will call Thing and Notification.


Defining the Thing Entity

I am going to create an entity called Thing to represent the things my app will track. It will have the following attributes:


  1. name
  2. startDate
  3. recordDayCount
  4. dateTypeRaw
  5. color
  6. isPrivate



name’ will be a string and contain the name given to the ‘thing’ by the user. When a new 'thing' is added the app will automatically assign a default name in the form of 'New thing x' where x is the first number that creates a unique name. The user can then change the name to something more meaningful in the Edit view.

startDate’ will contain the date the current number of days will be counted from. It will initially be set to the date the ‘thing’ was create. The user will be able to change this in the Edit view.

recordDayCount’ will be a 16 bit integer and hold the maximum number of days the Thing has ever reached without a reset.

dateTypeRaw’ will be a 16 bit integer value. My app will convert this raw value to a Swift Enumeration value. This will define how the thing is tracked. This value will define how the   app will display the Thing in the main view. It will default to 'When' and the user will have an option to change it in the Edit view.

'color' is also a 16 bit integer value. My app will convert this raw value into one of a predefined set of theme colors which will be used to display the thing in the main view.

'isPrivate' will be a Boolean value. This will control if the app displays the thing in the main view or only in the list view.

Adding the Thing Entity

To add the ’Thing’ entity I’m going to select my model in the project navigate and then click 'Add Entity' on the toolbar at the bottom of the main section. 



Next I click twice on the new entities name to change it to ‘Thing’.




Adding the Thing's Attributes

Attributes are added by clicking the + sign in the Attribute section.


name

The name attribute is of type string.



It is not optional and does not have a default value. When my app creates a new Thing object it calculate the next available unique name and assign it to this attribute.

startDate

The startDate attribute is of type Date. This actually stores both Date and Time. This is important to note because the number of days will be counted as the number of whole days from that specific time.



The startDate attribute is not optional and does not have a default value. When my app creates a new Thing object it will assign the current Date and Time to this attribute.

recordDayCount

The recordDayCount attribute is of type Integer 16.



The recordDayCount is not optional or transient and has a default value of zero. My app will check if its value needs updating whenever it recalculates the currentDayCount.

dateTypeRaw

The dateTypeRaw attribute is of type Integer 16.

The dateTypeRaw attribute is not optional and its default value is zero. Within the app this will be converted to a Swift Enumeration type called DateType.


color

The color attribute is of type Integer 16.

The color attribute is not optional and its default value is 4. Within the app this will be converted to one of a predefined set of color objects.


isPrivate

The isPrivate attribute is of type Boolean.

The isPrivate attribute is not optional and its default value is false. This attribute controls whether the thing is included in the set of things that are displayed on the main view.


notifications

The notifications relationship is a 'to Many' relationship with the Notification entity which I will create next.



This relationship is optional and will only exist if the user adds notifications to a thing.


Defining the Notification Entity

I am going to create an entity called Notification to represent the things my app will track. It will have the following attributes:


  1. before
  2. days
  3. uuid
  4. thing

before

The before attribute is of type Boolean.

The before attribute is not optional and its default value is true. This attribute defines whether the notification is sent before or after the thing's target date.


days

The days attribute is of type Integer 16.

The days attribute is not optional and default to zero. This attribute defines how many day before or after the thing's target date the notification is sent.


uuid

The uuid attribute is of type String.

The uuid attribute is created automatically by the app when a new notification is added. The purpose of this attribute is to give each notification record a unique identity.


thing

The thing relationship is a 'To one' relationship.

Every notification record will be linked to one thing record.

Wrapping up

My data model is now defined. Before moving on I am going to run my app in the Simulator to make sure it still compiles and then commit it to source control.

In my next article  I will create the class wrapper for my Thing and notification entities.

Next:       A Simple Swift iOS App from Start to Finish - Creating the Data Model's Wrapper Class

No comments:

Post a Comment