Coredata

징니·2023년 9월 13일
0

Swift

목록 보기
3/9

CoreData는 데이터베이스가 아님. ORM 매핑 프레임워크가 맞는 의미, 데이터 저장에 관한 일종의 프레임워크

  1. CoreData Model 생성하기
  2. 필요한 Attribute 추가하고 Entity 수정하기
  3. Editor - Create NSManagedObject Subclass
  4. Mutiple 오류 발생

    4-1. Codegen - Manual/None

    4-2. CoreDataProperties 파일 삭제
  5. Todo 파일 생성
  6. Appdelegate 작성 - import CoreData
// MARK: - Core Data stack
    lazy var persistentContainer: NSPersistentContainer = {
        
        let container = NSPersistentContainer(name: "TodoModel")
        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)")
            }
        }
    }
profile
iOS 개발자입니다

0개의 댓글