swift ์ค์ต - 7 : To-do App
์ด๋ฒ ์ค์ต์ ํตํด ์ฃผ๋ก ๋ฐฐ์ด ๋ด์ฉ์ ๋ก์ปฌDB๋ฅผ ์ฌ์ฉํด์ ์ฑ์ ์ข ๋ฃํด๋ ๋ฐ์ดํฐ๊ฐ ๋จ์์๊ฒ ํ๋ ๊ฒ์ ๋ํด์ ๋ฐฐ์ ๋ค.
๋จผ์ ์์ฑ๋ณธ์ด๋ค.
์ด๋ฐ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ ๋ ์ ๋ชฉ์ด๋ ์ค์ ์์๋ฅผ ์ ํํ ์ ์๊ณ
์ ์ฅํ ๋ ๋ฑ๋ก ๋ ์ง๊น์ง ์ ์ฅํด์ ํ ์ด๋ธ๋ทฐ์ ๋ณด์ฌ์ค๋ค.
์์ ๋ ํ ์ ์๊ณ , ์ญ์ ๊น์ง ํ ์ ์๋ค.
(์ฑ ์์ฒด๋ฅผ ์ญ์ ํ๊ณ ์๋ก ๊น์ง ์๋ ํ) ์ฑ์ ์ข ๋ฃํ๊ณ
๋ค์ ์คํํด๋ ๋ฐ์ดํฐ๊ฐ ๋จ์์๋ค.
์ด๋ฌํ ๊ฒ์ ๊ตฌํํ๊ธฐ ์ํด ํ์ํ ๊ฒ์
CoreData๋ผ๋ ๊ฒ์ด๋ค.
ํ๋ก์ ํธ๋ฅผ ์์ฑํ ๋ CoreData๋ฅผ Embedํ๊ฒ ๋ค๊ณ ์ฒดํฌํด์ผ ํ๋ค.
๋๊ฒ ๋ง์ ์ผ์ ํด์ ๋ฐฐ์ด ๋ด์ฉ์ ๋ค ์ ๋ฆฌํ๊ธด ๋ฒ ์ฐจ๊ณ ๊ธฐ์ตํ๊ณ ์ถ์๊ฑฐ ๋ช๊ฐ๋ง ์ ๋ฆฌํ ๊ฑฐ๋ค ใ ใ ใ ใ
1. Local Database ์ฌ์ฉ
CoreData๋ฅผ ์ฌ์ฉํ๊ฒ ๋ค๊ณ ํ๋ฉด
์ ๋ ๊ฒ ๋ฐ์ xcdatamodel ํ์ฅ์๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ์๊ธด๋ค.
๊ฑฐ๊ธฐ์ ๋ค์ด๊ฐ๋ฉด ์ด๋ ๊ฒ ์ํฐํฐ ์ด๋ฆ์ ์ค์ ํ๊ณ , ์์ ์์ฑ๋ค์ ์ด๋ฆ - ํ์ ์ ์ค์ ํ ์ ์๋ค.
๊ทธ๋ฆฌ๊ณ ์ด์ ์ด๊ฑธ ์ฌ์ฉํ๊ธฐ ์ํด์ ์ดํด๋ด์ผ ํ ๊ฒ
ํ์์ ๋๋ฌ๋ณด์ง๋ ์์๋ AppDelegate ํ์ผ์ด๋ค.
๊ฑฐ๊ธฐ์์ Core Data Stack ๋ถ๋ถ๊ณผ Core Data Saving Support ๋ถ๋ถ์ ๋ณด๋ฉด ๋๋ค.
๋จผ์ Core Data Stack ๋ถ๋ถ์ ๋ณด๋ฉด
// 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: "ToDoApp")
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
}()
persistantContainer๋ผ๋ ๊ฒ์ด ์๋ ๊ฒ์ ๋ณผ ์ ์๋ค. ์๊ตฌ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ์ปจํ ์ด๋ ๋ผ๋ ๋ป์ด๋ค.
๊ทธ๋ฆฌ๊ณ
Core Data Saving support์์๋
// 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)")
}
}
}
์ด๋ ๊ฒ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ์ ์๋ ํจ์๋ฅผ ์ ๊ณตํ๋ค.
๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๋ถ๋ถ์
func saveTodo(){
guard let entityDescription = NSEntityDescription.entity(forEntityName: "TodoList", in: context) else{ return }
guard let object = NSManagedObject(entity: entityDescription, insertInto: context) as? TodoList else { return }
object.title = titleTextField.text
object.date = Date()
object.uuid = UUID()
object.priorityLevel = priority?.rawValue ?? PriorityLevel.level1.rawValue
let appdelegate = (UIApplication.shared.delegate as! AppDelegate)
appdelegate.saveContext()
}
์ด๋ฐ์์ผ๋ก ๊ตฌํํ ์ ์๋ค.
2. ์ฐ์ ์์ ํํ
(1) ๋ฒํผ์ ๋๋ฅผ ๋ ๋ง๋ค ์์ด ๋ณํด์ผ ํ๊ณ
(2) ๋๋ฅธ ์ฐ์ ์์์ ๋ฐ๋ผ ๊ฐ์ด ์ ์ฅ๋ผ์ผ ํ๊ณ
(3) ํ ์ด๋ธ ๋ทฐ์์ ๊ฐ๊ฐ์ ์์ผ๋ก ํ์ํด์ค์ผ ํ๋ค.
์์ ์ธ๊ฐ์ง ์ผ์ ํ๋ ค๋ฉด ๋ฒํผ๋ค์ด ๊ตฌ๋ถ๋ผ์ผ ํ๋ค.
๊ทธ๋์ ์คํ ๋ฆฌ๋ณด๋์์ Tag๋ถ๋ถ์ ๋ฒํธ๋ฅผ ์ง์ ํด์ค๋ค.
Low๋ 1 Normal์ 2 High๋ 3์ด๋ค.
enum PriorityLevel: Int64 {
case level1
case level2
case level3
}
extension PriorityLevel{
var color: UIColor{
switch self{
case .level1:
return .green.withAlphaComponent(0.4)
case .level2:
return .orange.withAlphaComponent(0.4)
case .level3:
return .red.withAlphaComponent(0.4)
}
}
}
๊ทธ๋ฆฌ๊ณ ViewController์ ์ด๋ ๊ฒ enum์ผ๋ก PriorityLevel ํ์ ์ ์ ์ธํด์ฃผ๊ณ
๊ฐ๊ฐ์ ์๋ ์ง์ ํ๋ค.
@IBAction func setPriority(_ sender: UIButton) {
switch sender.tag {
case 1:
priority = .level1
break
case 2:
priority = .level2
break
case 3:
priority = .level3
break
default:
break
}
makePriorityButtonDesign()
}
๊ทธ๋ฆฌ๊ณ DetailView์์ 3๊ฐ์ ๋ฒํผ ๋ชจ๋ setPriority ์ก์ ํจ์์ ์ง์ ํด ๋์ ๋ค์
ํ๊ทธ๋ฅผ ๋ณด๊ณ ๊ตฌ๋ถํด์ ๋ ๋ฒจ์ ๋ถ์ฌํ๊ณ
makePriorityButtonDesign() ํจ์๋ฅผ ํธ์ถํ๋ค.
func makePriorityButtonDesign(){
lowButton.backgroundColor = .clear
normalButton.backgroundColor = .clear
highButton.backgroundColor = .clear
switch priority {
case .level1:
lowButton.backgroundColor = priority?.color
case .level2:
normalButton.backgroundColor = priority?.color
case .level3:
highButton.backgroundColor = priority?.color
default:
break
}
๊ทธ ํจ์๋ ์ด๋ ๊ฒ ์๊ฒผ๋ค.
๋จผ์ ๋ฒํผ ๋ฐฐ๊ฒฝ ์์ ๋ค ํฌ๋ช ์ผ๋ก ๋ฐ๊ฟ๋๊ณ ์ ํ๋ ๋ ๋ง๋ค enum์์ ์ ์ฅํด ๋์ color ์์ฑ ๊ฐ์ ๋ฃ์ด์ค๋ค.
DB์ ๊ฐ์ ์ ์ฅํ ๋
loadedData.first?.priorityLevel = self.priority?.rawValue ?? PriorityLevel.level1.rawValue
์ด๋ ๊ฒ rawValue๋ฅผ ์ ์ฅํ๋ฉด ๋๋ค.
CoreData์ ๋ํ ์ ํ ๊ณต์ ๋ฌธ์์ด๋ค.
https://developer.apple.com/documentation/coredata/core_data_stack
Apple Developer Documentation
developer.apple.com