Tips for Data Persistence in iOS with Swift

Apr 16
15:38

2020

Chris Bateson

Chris Bateson

  • Share this article on Facebook
  • Share this article on Twitter
  • Share this article on Linkedin

This article mainly focuses on the techniques and methods exclusively used in the context of storing data locally in an iOS mobile app.

mediaimage

One of the most crucial features of apps today is data persistence. If you don’t quite understand why a fundamental definition of the concept will help. Data persistence refers to the mechanism of storage of data,Tips for Data Persistence in iOS with Swift Articles irrelevant of its type, to a disk. The point is to make sure that this data can then be retrieved later whenever users open the app and allow them to continue from precisely where they left off. You get it now, don’t you?

This guide, however, will focus on techniques and methods exclusively in the context of storing data locally in an iOS app. And before we get started with that, it is essential to understand that local storage in iOS apps is not the default setting. Instead, what happens is that all the constants and variables assigned in Swift are stored in memory by default. It means that when users open their apps again, they lose the progress they may have previously made. Thankfully, there are a variety of tools in the market that allow app developers to make sure that their users don’t get lost every time they entirely and close the app. So, we have listed some of the most popular tools one can use to achieve data persistence in iOS apps with Swift.

1. UserDefaults: This one allows the storage of default data about the app and users. It is not only simple to use but is also thread-safe and can be shared between apps and app extensions. Here’s an example to help you understand better:

class UserRepository {
enum Key: String, CaseIterable {
case name, avatarData
func make(for userID: String) -> String {
return self.rawValue + "_" + userID
}
}
let userDefaults: UserDefaults
// MARK: - Lifecycle
init(userDefaults: UserDefaults = .standard) {
self.userDefaults = userDefaults
}
// MARK: - API
func storeInfo(forUserID userID: String, name: String, avatarData: Data) {
saveValue(forKey: .name, value: name, userID: userID)
saveValue(forKey: .avatarData, value: avatarData, userID: userID)
}

func getUserInfo(forUserID userID: String) -> (name: String?, avatarData: Data?) {
let name: String? = readValue(forKey: .name, userID: userID)
let avatarData: Data? = readValue(forKey: .avatarData, userID: userID)
return (name, avatarData)
}

func removeUserInfo(forUserID userID: String) {
Key
.allCases
.map { $0.make(for: userID) }
.forEach { key in
userDefaults.removeObject(forKey: key)
}
}
// MARK: - Private
private func saveValue(forKey key: Key, value: Any, userID: String) {
userDefaults.set(value, forKey: key.make(for: userID))
}
private func readValue(forKey key: Key, userID: String) -> T? {
return userDefaults.value(forKey: key.make(for: userID)) as? T
}
}

2. Core Data: It can be employed as a framework for either persistence or in-memory cache. The best part is that Apple offers plenty of aids to fetch data and that developers can use it as a cache when it is configured in memory.

3. SQLite: Though this one is a relational database management system, it isn’t a client-server database engine. It can be used to execute specific data read/write optimizations — an ability that other tools often lack.

As you can see, provided you find the right tool, data persistence can be easily achieved while building iOS mobile apps, thus ensuring the optimal user experience.