Data Persistence On iOS - An Overview
Very often an app creates data that should be saved. In this article we’ll discuss the different possibilities for data persistence on iOS.
Hint: This post has been updated to Swift 3 and iOS 10
UserDefaults are a great way to save a small amount of data. For example, this could be some settings the user chooses in the app. It’s not intended to save a lot of user generated data though! For that use case there are other possibilities on iOS.
Instances of the following classes can be saved by using UserDefaults: NSData, NSString, NSNumber, NSData, NSArray and NSDictionary. But also simpler types like bools and integers can be saved in the UserDefaults. For more details, take a look at this blog post.
Like on MacOS, there is a keychain on iOS as well. It’s a very safe location to safe high sensible data like login data and passwords. The API is a little bit low level, but Apple has created a very good wrapper for using the keychain. It’s an Objective-C class, so you have to insert it in the bridging-header file. Furthermore, it’s not ARC code. So in order to use it, you have to disable ARC for these source files.
If your application have a huge amount of structured data, a database could be the perfect way. As we will discuss later, iOS provides a very high-level way to handle user generated data – Core Data.
However, if you are developing for more than just one platform, then it could be a good idea to share the same database structure on all devices, especially if the database should not only contain user generated data but other data as well, like a product catalog for example.
So if you want to handle the sqlite database on your own, you can use the sqlite3 C-library, which is very lightweight, fast and reliable.
Core Data is Apple’s persistency framework. It’s based on an object graph that describes the objects that should be saved. Then, you can directly work with these objects and the framework takes care on saving it to a database.
This sounds very straightforward, but at the beginning Core Data is very complex to use though. But it’s worth the learning effort because Core Data provides you a lot of possibilities. For example, you can perform undo and redo operations. It’s also very performant and Core Data lazy loads the data.
Of course you can also directly save all types of files to the file system. However, you can just access the file system within the app container due to security reasons. Basically there are three folders within the app container:
- Documents: This is the perfect place to save user generated content. It’s also backed up to the iCloud automatically.
- Library: In this folder you can put files that are not generated by the user and that should persist between launches of the app. However, some files could be deleted when the device doesn’t has enough free space. There are some subfolders within the library folder for different purposes. For more details take a look at the iOS file systems basics document.
- tmp: In this folder you can save files that the app just needs temporarily. They can be deleted by the operation system when the app is not running.
The NSData class provides reading and writing functionality.
Title Image: @ MilousSK / shutterstock.com