์ฑ๊ธํด ํจํด
์ฑ๊ธํด ํจํด์ ์ํํธ์จ์ด ๋์์ธ ํจํด ์ค ํ๋๋ก, ๋จ ํ๋์ ์ธ์คํด์ค๋ง์ ์ฌ์ฉํ๋ ๋ฐฉ์์ ๋๋ค.
๋ฐ๋ผ์ ํ ๋ฒ ์์ฑ๋๋ฉด ๊ทธ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ๋ง ๊ณ์ ์ฌ์ฉํ๋ฏ๋ก ๋ฉ๋ชจ๋ฆฌ ๋ญ๋น๋ฅผ ๋ฐฉ์งํ ์ ์๊ณ , ์ฌ์ฉ ์ ์ด๋ฏธ ์์ฑ๋์ด ์๋ ์ธ์คํด์ค ์ฌ์ฉํ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์๋๋ ๋น ๋ฆ ๋๋ค.
ํ์ง๋ง ์ ์ญ์ ์ผ๋ก ์์ฑ๋์ด ๋ง์ ๊ณณ์์ ์ฌ์ฉ๋๋ฏ๋ก ์ฌ๋ฌ ์ธ์คํด์ค๊ฐ ์์ฑ๋๋ ๋ฑ์ ๋ฉํฐ์ค๋ ๋ฉ ํ๊ฒฝ์์ ๋์์ฑ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค. ๋คํํ swift์์๋ ์ด๊ธฐํ ์์ ์ ๋ฏธ๋ฃฌ(lazily) static ํ๋กํผํฐ๋ฅผ ํตํด ์ธ์คํด์ค๋ฅผ thread-safetyํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
The free function dispatch_once is no longer available in Swift. In Swift, you can use lazily initialized globals or static properties and get the same thread-safety and called-once guarantees as dispatch_once provided.
๊ทธ๋ผ์๋ ์ฑ๊ธํด ํจํด ์ฌ์ฉ์ ์ ์คํด์ผํฉ๋๋ค. ์์กด ๊ด๊ณ์ ๊ตฌ์ฒดํ๋ ํด๋์ค์ ์์กดํ๊ฒ ๋๋ฏ๋ก, ๊ฐ์ฒด ์งํฅ ์ค๊ณ ์์น์ ์๋ฐ๋๊ฒ ๋ฉ๋๋ค.
๊ฐ์ฒด ์งํฅ ์ค๊ณ ์์น์ ์์ง ๊ณต๋ถ๊ฐ ํ์ํ ๋ถ๋ถ์ด๊ธฐ ๋๋ฌธ์ ์ฌ๊ธฐ๊น์ง๋ง ์ ๋ฆฌํ๊ฒ ์ต๋๋ค.
์ฑ๊ธํด ํจํด ์ฌ์ฉ ์์
iOS์์๋ ๋ค์ํ ๊ณณ์์ ์ฑ๊ธํด ํจํด์ด ์ฌ์ฉ๋๊ณ ์์ผ๋ฉฐ ์ฌ์ฉ๋ฒ์ผ๋ก ๊ถ์ฅํ๊ธฐ๋ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด CoreData๋ฅผ ์ฌ์ฉ ์ ์ฑ๊ธํด ํจํด์ผ๋ก CoreDataStack์ ์์ฑํ์ฌ ์ฑ ์ ์ญ์์ CoreData์ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๋๋ก ์ ์ํ๊ณ ์์ต๋๋ค.
// Define an observable class to encapsulate all Core Data-related functionality.
class CoreDataStack: ObservableObject {
static let shared = CoreDataStack() // โญ๏ธ
// Create a persistent container as a lazy variable to defer instantiation until its first use.
lazy var persistentContainer: NSPersistentContainer = {
// Pass the data model filename to the containerโs initializer.
let container = NSPersistentContainer(name: "DataModel")
// Load any persistent stores, which creates a store if none exists.
container.loadPersistentStores { _, error in
if let error {
// Handle the error appropriately. However, it's useful to use
// `fatalError(_:file:line:)` during development.
fatalError("Failed to load persistent stores: \(error.localizedDescription)")
}
}
return container
}()
private init() { }
}
Setting up a Core Data stack | Apple Developer Documentation
Set up the classes that manage and persist your appโs objects.
developer.apple.com
Class Singleton ๐ Struct Singleton
์ Apple์ ์์ ์ฝ๋์์๋ ์ ์ ์๋ฏ์ด, ์ฑ๊ธํด ํจํด์ ํด๋์ค๋ฅผ ํตํด ๊ตฌํํ ์ ์์ต๋๋ค. ๊ทธ๋์ผ ๋จ ํ๋์ ์ธ์คํด์ค๋ก ์ฑ ์ ์ญ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ํด๋์ค์ ๊ตฌ์กฐ์ฒด์ ์ฐจ์ด์ ์ผ๋ก, ๊ฐ๋จํ๊ฒ ์ฐธ์กฐ ํ์ ๊ณผ ๊ฐ ํ์ ์ ๋ค ์ ์์ต๋๋ค. ์ฑ๊ธํด ํจํด์ ํด๋์ค๊ฐ ์ฌ์ฉ๋๋ ์ด์ ๋ ์ด ์ฐจ์ด์ ์์ ์์๋ผ ์ ์์ต๋๋ค.
ํด๋์ค์ ๊ฒฝ์ฐ, ์ธ์คํด์ค ์์ฑ ์ ํ ์์ญ์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ํ ๋น๋์ด ๋ณ์/์์๊ฐ ์ด ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ฐฉ์์ ๋๋ค. ๋ฐ๋ผ์ A ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ `shared` ๋ณ์๊ฐ `a` ๋ณ์์ ์์ ์ ํ ๋นํ๋ฉด, ์์ ์ด ๊ฐ์ง๊ณ ์๋ ์ธ์คํด์ค์ ์ฃผ์ ๊ฐ์ ์ ๋ฌํ๊ฒ ๋๋ฉฐ `shared` ์ `a` ๋ณ์ ๋ชจ๋ ๊ฐ์ A ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋ฉ๋๋ค.
์ด์ ๋ฐ๋๋ก ๊ตฌ์กฐ์ฒด์ ๊ฒฝ์ฐ, ์ธ์คํด์ค ์์ฑ ์ ์คํ ์์ญ์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ํ ๋น๋์ด ๋ณ์/์์๊ฐ ์ธ์คํด์ค๋ฅผ ๊ฐ์ง๊ณ ์๋ ๋ฐฉ์์ ๋๋ค. ๋ฐ๋ผ์ B-0 ์ธ์คํด์ค๋ฅผ ๊ฐ์ง `shared` ๋ณ์๊ฐ `b` ๋ณ์์ ์์ ์ ํ ๋นํ๋ฉด ์๋ก์ด B-1 ์ธ์คํด์ค ํ ๋น๋๊ฒ ๋ฉ๋๋ค. ๋ฐ๋ผ์ ๊ฐ๊ธฐ ๋ค๋ฅธ ์ธ์คํด์ค๋ฅผ ๊ฐ๊ฒ ๋์ด ๋์ผํ ์ธ์คํด์ค๋ฅผ ์ฌ์ฉํด ์ฑ ์ ์ญ์์ ๊ณต์ ๋ฐ ์ฌ์ฉํ๊ณ ์ ํ๋ ์ฑ๊ธํด ํจํด์ ๋ชฉ์ ์ ์ด๋ฃจ์ง ๋ชปํ๊ฒ ๋ฉ๋๋ค.

/*
ํด๋์ค๋ฅผ ์ฌ์ฉํ ์ฑ๊ธํด ํจํด
*/
class SingletonWithClass {
static let shared = SingletonWithClass()
var value: Int = 10
private init() {}
}
let a = SingletonWithClass.shared
a.value = 100
print("SingletonWithClass.shared", SingletonWithClass.shared.value)
print("a", a.value)
/*
์ถ๋ ฅ
SingletonWithClass.shared 100
a 100
*/
// ๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํ ์ฑ๊ธํด ํจํด
struct SingletonWithStruct {
static let shared = SingletonWithStruct()
var value: Int = 10
private init() {}
}
var b = SingletonWithStruct.shared
b.value = 100
var c = SingletonWithStruct.shared
c.value = 200
print("SingletonWithStruct.shared", SingletonWithStruct.shared.value)
print("b", b.value)
print("c", c.value)
/*
SingletonWithStruct.shared 10
b 100
c 200
*/
์ฑ๊ธํด ํจํด์ ์ฅ๋จ์
thread-safetyํ ์ฑ๊ธํค ์ฌ์ฉ
'Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ Swift ] ์๋ฌ ํธ๋ค๋ง (0) | 2025.02.12 |
---|---|
[ iOS ] (0) | 2025.02.07 |
[ Swift ] Swift๋? (0) | 2023.10.08 |
[ Swift ] zip(_:_:) (1) | 2023.10.08 |
[ Swift ] isCase | isLowercase | isUppercase (0) | 2023.10.04 |