iOS Tips: Data Persistence
In this article, I would like to talk about ways of persisting data in iOS apps.
Persistence in computer science is defined as ‘the characteristic of state that outlives the process that created it’.
Data persistence means storing any type of data on disk so that the same data can be retrieved unchanged the next time the user opens the application. I will explain all the ways to store data locally in iOS apps with the keywords and short explanations.
- User Defaults
- File Manager
- Core Data
User Defaults → key/value storage. For small informations. Great for user settings or small amounts of JSON. We can store integers, booleans, strings, arrays, dictionaries, dates or NSData for other types. Caches the information. Thread-safe.
Keychain → key/value storage. Storage for small bits of sensitive user data, encryted database. Not Thread-safe.
File Manager → Working with network like API. create , delete , update and read your app’s files with full control. Any apps get a directory of own. Can be called Thread-Safe. Using delegate to receive move, copy, remove, and link updates, can break thread safe.
Core Data → Model layer object. Filter, modify, save, track. Rely on an SQLite back end. Potentially blocking the UI with data tasks, like parsing JSON into objects, in the background. Designed to work in a multithreaded environment. However, not every object under the Core Data framework is thread safe.
SQLite → Relational DB, not a client server, embed in app, no networking. Uses a query language. Not Thread-safe.
PList → Serialized objects with key/value conventions. Unencrypted.
Realm → Unlike wrappers around Core Data, Realm doesn’t rely on Core Data or even an SQLite back end. Solves Local storage, Network reliability, Reactive UI.
NSURLCache → Caching of responses to URL load requests, by mapping
NSURLRequest objects to
NSCachedURLResponse objects. Provides in-memory and on-disk cache, and sizes of both can be manipulated. Thread safe.