μ±μ κ°μ²΄λ₯Ό κ΄λ¦¬νκ³ μμννλ ν΄λμ€λ₯Ό μ€μ
Core Data μ€ν(Core Data stack)
NSManagedObjectModel
μ μΈμ€ν΄μ€λ μ±μ λͺ¨λΈ νμΌμ λνλ΄λ©°, μ±μ μ ν, μμ± λ° κ΄κ³λ₯Ό μ€λͺ
ν¨NSManagedObjectContext
μ μΈμ€ν΄μ€λ μ±μ μ νμ μΈμ€ν΄μ€μ λν λ³κ²½ μ¬νμ μΆμ ν¨NSPersistentStoreCoordinator
μ μΈμ€ν΄μ€λ μ±μ μ νμ μΈμ€ν΄μ€λ₯Ό μ μ₯μμμ μ μ₯νκ³ κ²μν¨NSPersistentContainer
μ μΈμ€ν΄μ€λ λͺ¨λΈ, 컨ν
μ€νΈ λ° μ€ν μ΄ μ½λλ€μ΄ν°λ₯Ό ν λ²μ μ€μ ν¨Core Data
μ μ£Όμ ν΄λμ€λ€λ‘, λ°μ΄ν°λ₯Ό κ΄λ¦¬νκ³ μμμ±μ μ 곡νλ λ° μ¬μ©λ¨Persistent Container
μ μ΄κΈ°ν
Core Data
λ₯Ό μ΄κΈ°νν¨lazy
λ³μλ‘ μꡬ 컨ν
μ΄λλ₯Ό μμ±ν¨Xcode
νλ‘μ νΈλ₯Ό λ§λ€ λ Core Data
체ν¬λ°μ€λ₯Ό μ ννλ€λ©΄, ν΄λΉ ν
νλ¦Ώμ AppDelegate
μ μ΄ μ΄κΈ° μ€μ μ½λλ₯Ό μλμΌλ‘ ν¬ν¨μν΄NSPersistentContainer
νμ
μ μ§μ° λ³μλ₯Ό μ μΈpersistent stores
λ₯Ό λ‘λ νλ©΄, μ μ₯μκ° μλ κ²½μ°μ μ μ₯μλ₯Ό μμ±ν¨class AppDelegate: UIResponder, UIApplicationDelegate {
...
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "DataModel")
container.loadPersistentStores { description, error in
if let error = error {
fatalError("Unable to load persistent stores: \(error)")
}
}
return container
}()
...
}
Persistent Container
λ ν΄λΉ managedObjectModel
, viewContext
, κ·Έλ¦¬κ³ persistentStoreCoordinator
μμ±μ ν΅ν΄ λͺ¨λΈ, 컨ν
μ€νΈ, κ·Έλ¦¬κ³ μ μ₯μ μ½λλ€μ΄ν° μΈμ€ν΄μ€μ λν μ°Έμ‘°λ₯Ό μ μ§ν¨μꡬ 컨ν μ΄λ μ°Έμ‘°λ₯Ό λ·° 컨νΈλ‘€λ¬μ μ λ¬
Core Data
λ₯Ό κ°μ Έμ€κ³ μꡬ 컨ν
μ΄λμ λν μ°Έμ‘°λ₯Ό 보μ ν λ³μλ₯Ό λ§λ€μ΄μΌ ν¨import UIKit
import CoreData
class ViewController: UIViewController {
var container: NSPersistentContainer!
override func viewDidLoad() {
super.viewDidLoad()
guard container != nil else {
fatalError("This view needs a persistent container.")
}
// The persistent container is available.
}
}
appβs delegate
λ‘ λμκ° λ€, application(_:didFinishLaunchingWithOptions:)
λ©μλ λ΄μμ μ± μ°½μ rootViewController
λ₯Ό μ±μ λ£¨νΈ λ·° 컨νΈλ‘€λ¬ νμ
μΌλ‘ λ€μ΄μΊμ€νΈ ν΄μΌν¨container
μμ±μ μꡬ 컨ν
μ΄λλ‘ μ€μ ν΄μΌν¨class AppDelegate: UIResponder, UIApplicationDelegate {
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let rootVC = window?.rootViewController as? ViewController {
rootVC.container = persistentContainer
}
return true
}
...
}
Persistent Container
λ₯Ό μΆκ°μ μΈ λ·° 컨νΈλ‘€λ¬λ‘ μ λ¬νλ €λ©΄ κ° λ·° 컨νΈλ‘€λ¬μμ 컨ν
μ΄λ λ³μλ₯Ό μμ±νκ³ , μ΄μ λ·° 컨νΈλ‘€λ¬μ prepare(for:sender:)
λ©μλμμ ν΄λΉ κ°μ μ€μ ν΄μΌν¨class ViewController: UIViewController {
...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let nextVC = segue.destination as? NextViewController {
nextVC.container = container
}
}
}
NSPersistentContainer
λ μλΈν΄λμ€νλ κ²μ κ³ λ €ν κ² Core Data
κ΄λ ¨ μ½λλ₯Ό ν¬ν¨ν λ°μ΄ν° μλΈμ
μ λ°ννλ ν¨μλ, λ°μ΄ν°λ₯Ό λμ€ν¬μ μ μ₯νλ νΈμΆκ³Ό κ°μ κΈ°λ₯λ€μ νΈλ¦¬νκ² μΆκ°ν μ μλ μ₯μμimport CoreData
class PersistentContainer: NSPersistentContainer {
func saveContext(backgroundContext: NSManagedObjectContext? = nil) {
let context = backgroundContext ?? viewContext
guard context.hasChanges else { return }
do {
try context.save()
} catch let error as NSError {
print("Error: \(error), \(error.userInfo)")
}
}
}
saveContext
ν¨μλ₯Ό μΆκ°ν μ½λμ[πApple Docs: Setting up a Core Data stack]
https://developer.apple.com/documentation/coredata/setting_up_a_core_data_stack#overview