๋ค์ด์ด๋ฆฌ ์ฑ์ ๋ง๋ค๋ฉด์ ๊ธฐ์กด์ ํ์ด์ด ๋ฒ ์ด์ค๋ฅผ ํตํด ์ ์ฅํ๋ ค๊ณ ํ๋ ๋ฐ์ดํฐ๋ฅผ ์์ ๋ก๊ทธ์ธ ๊ธฐ๋ฅ์ ์ ์ธ์ํค๋ฉด์ ๊ตณ์ด..? ๋ผ๋ ์๊ฐ์ด ๋ค์ด ์ด์ฐจํผ ๋ก๊ทธ์ธ์ด ํ์ ์์ผ๋๊น ๊ทธ๋ฅ ์ฑ ๋ด์ ์ ์ฅํด๋ ๋๊ฒ ๋ค! ์ถ์ด์ ์ ์ ๋ดค์๋ CoreData๋ฅผ ์ฌ์ฉํด๋ณด๊ฒ ๋์๋ค! (์คํ๋ ค ํ์ด์ด๋ฒ ์ด์ค๋ฅผ ์ฐ๋ฉด ์ธํฐ๋ท๋ ๋์ผํ๋๊น ๋ ๋ถํธํ ๊ฒ ๊ฐ๋ค..~!)
CoreData๋ UserDefaults๋ฅผ ์ฌ์ฉํ๋ฉด์ ์๊ฒ ๋์๋๋ฐ, ์ ์ ๋ด๊ฐ ์ฌ์ฉํ๋ ์ ์ด ์๋ UserDefaults๋ ๊ฐ๋จํ ๋ฐ์ดํฐ(?)๋ฅผ ์ ์ฅํ๋ ์ฉ๋๋ก ์ฌ์ฉํ๊ณ , CoreData๋ ๊ทธ๋ณด๋ค ๋ ํฐ ๊ท๋ชจ์ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃฌ๋ค๊ณ ํ๋ค!
์ ๋ด๊ฐ ์๊ฐํ๊ธฐ์ ์๋ฅผ๋ค์๋ฉด ๋ก๊ทธ์ธ์ ํ๋์ง ์ํ๋์ง์ ๋ํ ์ฌ๋ถ.. ์๊ฐ์ ๊ฐ๋จํ ์ ๋ณด๋ UserDefaults, ๊ทธ๋ฆฌ๊ณ ๋ด๊ฐ ๊ฐ๋ฐ์ค์ธ ๋ค์ด์ด๋ฆฌ ๋ด title, contents์ ๊ฐ์ ์ ๋ณด๋ค์ ๊ทธ๋๋ง ์ข ํฌ๋๊น CoreData๋ก ์๊ฐํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค!
์์ ์ ์ฒ์ iOS๋ฅผ ๋ฐฐ์ธ ๋ ํ๋ก์ ํธ๋ฅผ ์์ฑํ๋ ๋ถ๋ถ์์
์ด ํ๋ฉด์ ๋ํ ๋ด์ฉ์ ์ค๋ช ํด ์ฃผ์๋๋ฐ ์๋ ์ฒดํฌ๋ฐ์ค ๋ถ๋ถ์ ์ ๊ฒฝ์ฐ์ง ๋ง๋ผ๊ณ ํ์๋ค.
๊ทผ๋ฐ ์ด๋ฒ์ CoreData๋ฅผ ๋ฐฐ์ฐ๋ฉด์ ์ ์ฒดํฌ๋ฐ์ค์ ์กด์ฌ ์ด์ ๊น์ง ์๊ฒ ๋์๋ค!
์ ์ฒดํฌ๋ฐ์ค๋ฅผ ์ฒดํฌํ ํ ํ๋ก์ ํธ๋ฅผ ์์ฑํ๋ฉด ๋๋ CoreData๋ฅผ ์ฌ์ฉํ๊ฒ ๋ค๊ณ ํ๊ณ ํ๋ก์ ํธ๋ฅผ ์์ฑํ๋ค.
๊ทธ๋ฌ๋ฉด ๊ธฐ์กด์ ๊ทธ๋ฅ ์์ฑํ ๋์ ๋ค๋ฅด๊ฒ
๊ฐ์ฅ ์๋์ ์๋ฐ ํ์ผ์ด ํ๋ ๋ ์๊ธด๋ค!
๊ทธ๋ฆฌ๊ณ AppDelegate๋ ์ฝ๊ฐ ๋ค๋ฅธ๋ฐ,
import UIKit
import CoreData
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "tmp")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}
๊ทธ๋๋ก ๋ฐฐ๊ปด์ค์๋ฉด, CoreData์ ํด๋นํ๋ ์ฝ๋๊ฐ ์ถ๊ฐ๋๊ณ , CoreData๊ฐ import ๋์ด์๋ค.
๋ง์ฝ ํ๋ก์ ํธ๋ฅผ ์ด๋ฏธ ์์ฑํ ํ CoreData๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค๋ฉด, Command+n์ ํตํด ํ์ผ์ ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค. (์ด๋ ํ์ผ ๋ช ์ AppDelegate์ container๋ถ๋ถ์ name๊ณผ ๊ฐ์์ผํ๋ค)
https://developer.apple.com/documentation/coredata/setting_up_a_core_data_stack
์ ๋งํฌ๋ก ์ด๋ํ๋ฉด ์์ ๊ฐ์ ๊ทธ๋ฆผ์ ํ์ธํ ์ ์๋๋ฐ, CoreData์ ๊ตฌ์กฐ์ด๋ค.
-> ์ธ์คํด์ค๋ ์ฑ์ ์ ํ, ์์ฑ ๋ฐ ๊ด๊ณ๋ฅผ ์ค๋ช ํ๋ ์ฑ์ ๋ชจ๋ธ ํ์ผ์ ๋ํ๋ ๋๋ค
-> ์ธ์คํด์ค๋ ์ฑ ์ ํ์ ์ธ์คํด์ค์ ๋ํ ๋ณ๊ฒฝ ์ฌํญ์ ์ถ์ ํฉ๋๋ค
3. NSPersistentStoreCoordinator
-> ์คํ ์ด์์ ์ฑ ์ ํ ์ ์ธ์คํด์ค๋ฅผ ์ ์ฅํ๊ณ ๊ฐ์ ธ์ค๋ ์ธ์คํด์ค์ ๋๋ค.
-> ์ธ์คํด์ค๋ ๋ชจ๋ธ, ์ปจํ ์คํธ ๋ฐ ์์ ์ฝ๋๋ค์ดํฐ๋ฅผ ํ ๋ฒ์ ์ค์ ํฉ๋๋ค.
๋ผ๊ณ ์ ํ์๋๋ฐ ใ ใ ๋ ๋ฌด์จ์๋ฆฌ์ธ์ง ๊ฐ์ด ์์์ ๋ ์ฐพ์๋ดค๋ค.
๊ฐ๋จํ ์ ๋ฆฌํด๋ณด๋ฉด
1. ๋ชจ๋ธํ์ผ์ธ NSManagedObjetContext์์ Persistent Container๋ฅผ ์์ฑํ๋ค.
2. entity๋ฅผ ๊ฐ์ ธ์จ๋ค.
3. NSManagedObject๋ฅผ ๋ง๋ค์ด ๊ฐ์ ์ธํ ํ๋ค.
4. NSManagedObjectContext์ saveํ๋ค.
์ฒ์ ํด๋ณด๋๊ฑฐ๋ผ ๊ฐ์ด ์ ์์ค์ง๋ง.. CoreData ๊ตฌ์กฐ์ ๊ธฐ๋ฐํ์ฌ ์ญํ ์ ์๊ฐํ๋ฉฐ ๋ณด๋ฉด ์ด๋ ดํ์ด.. ์ดํดํ ์ ์์ ๊ฒ ๊ฐ๋ค.
๋๋ ๊ฐ๋จํ ๊ตฌ์กฐ์ ๋ฐ์ดํฐ ๊ด๋ฆฌ๋ผ์ ํฌ๊ฒ ๋ณต์กํ์ง ์์๋๋ฐ ๋ ๋ณต์กํ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฉด ๋ง์ด ๊ณ ๋ฏผํ๊ณ ์ฐ์ตํด์ผ๊ฒ ๋ค..๐ค๐ฅบ
'iOS > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[iOS/Swift] Convenience init (0) | 2023.04.21 |
---|---|
[iOS] ํ์ ์ฃผ์(Type Annotation)๊ณผ ํ์ ์ถ๋ก (Type Inference) (0) | 2022.12.27 |
[iOS/swift] navigation controller์์ root view ์ง์ (์คํ ๋ฆฌ๋ณด๋ ๊ตฌ์ฑ) (0) | 2022.08.20 |
Reactive X ๊ณต๋ถ (0) | 2022.07.19 |
[iOS/Swift] TableView ๋ง๋ค๊ธฐ (0) | 2022.03.21 |