2022-05-12
[Swift] How to keep data in an app with a singleton class.
Table of contents
Overview
Hello, I'm Ishiyama, Web Engineer!
In this article, we will explain Singleton, one way to hold data in an app in iOS development, and how to implement it in Swift.
There are various methods of data retention in iOS app development.
- "Data to determine if it is the first time logging in (if it is the first time, the tutorial screen is displayed)."
- "screen information that was viewed the last time the app was launched (enabling "continue from where you left off" when the app is launched again)."
- "I want to retain the input data (without using an external DB)."
- "I want to keep my login information."
As mentioned above, there are various situations where you want to retain data, and if you want to retain data semi-permanently, you often use UserDefaults, KeyChain, Database, etc., and implement them depending on the usage and requirements.
What is Singleton?
Singleton is a design pattern that "designs a class to always have a single instance of that class at runtime.
Only one instance of a singleton-implemented class can exist in an app, and it can be forced to always refer to the same instance, no matter where it is referenced.
In other words, it can be used as in-app data that can be held as in-app data and referenced from any ViewController.
In what situations is it used?
- User information after login
- For apps that have a web version as well as an iOS app, it is possible to change user names, user IDs, etc. on the web, so the login API and user information acquisition API may always be executed at app startup.
In such cases, if the information is kept in semi-permanent UserDefaults, etc., it may not reflect information that has been changed on the Web side, and the old information may be displayed in the application, so singleton classes that are kept during application startup may be used rather than semi-permanent.
Mounting Method
Now, let's implement a class that holds user information as a singleton.
// Class to hold user information
final class UserInfo {
// Properties for referencing instances
static let shared = UserInfo()
// user id
var id: String = ""
// user name
var name: String = ""
// initialize
private init() {}
// user information set method
public static func setUserInfo(id: String, name: String) {
self.id = id
self.name = name
}
}
// =============================================================
// At the completion of execution of some login API or user information acquisition API
userInfoStream.listen { response in
// set to UserInfo
UserInfo.setUserInfo(id: response.id, name: response.name)
}
// Another ViewController, for example.
let id = UserInfo.shared.id
let name = UserInfo.shared.name
print("[ID: \(id), Name: \(name)]")
- Point.1: class to final
-
final class UserInfo {
Singletons should be final to prevent inheritance. - Point.2: private init
-
private init() {}
Singleton classes must be private so that they cannot be instantiated by others, since there must be one in the app. - Point.3: Create public static shared property
-
static let shared = UserInfo()
Create a public, static shared property to hold an instance of itself (singleton class).
Refer to properties (user IDs and user names) in the singleton class via this shared property.
Summary
In this article, I explained one method of data retention in iOS applications, Singleton class, and how to implement it.
I would like to write more articles on UserDefaults, KeyChain, DB, etc. again, please let me know.