Persistent Data in Swift

Whenever data is created in a Swift app, it is loaded into memory. However, memory is temporary storage and if the user ever closes your app (which will eventually happen) that data will be cleared to make space for another app that needs that space in memory for its data. Therefore we need some sort of process for storing data in a way that allows it to persist any different life cycles of an application.

This is where the idea of Persistent Data is valuable. To make data persist in an app, it needs to be written somewhere in a device’s directory. This is the same as when you create a file on your laptop and save it in your Documents folder. That way when you restart your computer it will still be there!

Swift makes this process very easy by providing utils that handle the storage and encoding/decoding of our apps. It is much simpler than managing a SQL database and allows developers to save and load their data models on the fly!

Documents Directory

iOS gives developers space to store data for their apps. In order to gain a reference to this directory in the swift code it is useful to create a method that you can call to get this path.

Screen Shot 2016-10-07 at 11.07.28 AM.png

The first line below creates an array of URL objects. It does this by using the FileManager object to retrieve urls by using the .urls(for:, in:) method where for: is the directory you would like to look for and in: is the domain in which to look for. We would like to look for the Documents directory in the User domain so the code below will suffice.

Screen Shot 2016-10-07 at 11.11.32 AM.png

We then return the first element of the array which contains a URL reference to the Documents directory.

Screen Shot 2016-10-07 at 11.15.38 AM.png

This second method appends a filename to the end of our directory to save our data in.

Screen Shot 2016-10-07 at 11.20.36 AM.png

This method will return the full path with the FileName.plist file appended at the end of it. We will use this dataFilePath() method for reference when we are saving and loading data in our app.

.plist files

.plist files are a filetype that iOS uses to store information about an app. It is an XML file that stores information in the form of a Dictionary which uses key/value pairings. You might have noticed that when creating a Swift application in XCode, you are given an info.plist file. This is another .plist file that holds information about your app such as supported orientations of your app and the bundle name associated with your app. For the purposes of saving data in an app we are using a .plist file to save Swift objects.

Saving Data

We will be saving an array of a data type Item to the .plist file.

Screen Shot 2016-10-07 at 11.47.11 AM.png

It is nice to create a handy method like saveItems() that can be called whenever we need to save data.

Screen Shot 2016-10-07 at 11.32.25 AM.png

In order to save our data, it first needs to be encoded. To encode the data, we have to create an NSKeyedArchiver object. This archiver requires an NSMutableData() object to write the encoding to.

Screen Shot 2016-10-07 at 11.31.01 AM.png

The next step is the actual encoding. We simply call the .encode(forKey:) method on the archiver which takes the data that you would like to encode and a key. This key is used to identify the object when we need to load it back later. Then just call finishEncoding(). After the encoding we can easily call the .write(to:, atomically:) method on our data object where :to requires a path to where we would like to write our data, for this we can call our handy dataFilePath() method. The parameter atomically: is set to true which writes our data to a backup location.

Screen Shot 2016-10-07 at 11.44.48 AM.png

Loading Data

Loading data from the .plist file is very easy. We will create another handy method for loading data.

Screen Shot 2016-10-07 at 12.03.46 PM.png

We first get a reference to the file path of our .plist file by calling our dataFilePath() method.

Screen Shot 2016-10-07 at 11.53.08 AM.png

Then the try? tries to create a new Data object, if it cannot then it will return nil, resulting in the block of code not executing. Creating the Data object returns nil if the path we gave it does not exist. (Swift forces us to be cautious of situations like this with optionals) If we are able to create that Data object then we create a NSKeyedUnarchiver to decode our stored object and give it our data object as a constructor parameter. Note that when we call the .decodeObject() method we give it the key we used to store our object earlier. Also, we must cast the returned value to our data type by using the as! (forced optional unwrap) operator since Swift cannot infer what the type of the encoded object should be. The resulting value of the decoding is stored back in our global array of Items. Lastly we call .finishDecoding().

Screen Shot 2016-10-07 at 12.10.09 PM.png

Using the Save/Load Methods

These methods are super handy because we can throw then anywhere in our app! If the values of our data are modified we can simply call saveItems() and when that app is starting up we can call loadItems(). Also, any time that our app is interrupted (which happens all the time) we can call a save to make sure no data is lost!