{{announcement.body}}
{{announcement.title}}

Tips for Data Persistence in iOS With Swift

DZone 's Guide to

Tips for Data Persistence in iOS With Swift

In this article, we discuss some tips on how to best persist data with Swift in iOS applications.

· Web Dev Zone ·
Free Resource

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, 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. 

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.

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:

Swift
 




x
79


 
1
class UserRepository {
2
 
          
3
    enum Key: String, CaseIterable {
4
 
          
5
        case name, avatarData
6
 
          
7
        func make(for userID: String) -> String {
8
 
          
9
            return self.rawValue + "_" + userID
10
 
          
11
        }
12
 
          
13
    }
14
 
          
15
    let userDefaults: UserDefaults
16
 
          
17
    // MARK: - Lifecycle
18
 
          
19
    init(userDefaults: UserDefaults = .standard) {
20
 
          
21
        self.userDefaults = userDefaults
22
 
          
23
    }
24
 
          
25
    // MARK: - API
26
 
          
27
    func storeInfo(forUserID userID: String, name: String, avatarData: Data) {
28
 
          
29
        saveValue(forKey: .name, value: name, userID: userID)
30
 
          
31
        saveValue(forKey: .avatarData, value: avatarData, userID: userID)
32
 
          
33
    }
34
 
          
35
    
36
 
          
37
    func getUserInfo(forUserID userID: String) -> (name: String?, avatarData: Data?) {
38
 
          
39
        let name: String? = readValue(forKey: .name, userID: userID)
40
 
          
41
        let avatarData: Data? = readValue(forKey: .avatarData, userID: userID)
42
 
          
43
        return (name, avatarData)
44
 
          
45
    }
46
 
          
47
    
48
 
          
49
    func removeUserInfo(forUserID userID: String) {
50
 
          
51
        Key
52
 
          
53
            .allCases
54
 
          
55
            .map { $0.make(for: userID) }
56
 
          
57
            .forEach { key in
58
 
          
59
                userDefaults.removeObject(forKey: key)
60
 
          
61
        }
62
 
          
63
    }
64
 
          
65
    // MARK: - Private
66
 
          
67
    private func saveValue(forKey key: Key, value: Any, userID: String) {
68
 
          
69
        userDefaults.set(value, forKey: key.make(for: userID))
70
 
          
71
    }
72
 
          
73
    private func readValue<T>(forKey key: Key, userID: String) -> T? {
74
 
          
75
        return userDefaults.value(forKey: key.make(for: userID)) as? T
76
 
          
77
    }
78
 
          
79
}



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.

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 apps, thus ensuring the optimal user experience.

Topics:
data persistance ,data persistence ,ios ,mobile app development ,swift ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}