본문 바로가기

iOS 앱 개발자 프로젝트/iOS 숙련

[iOS] Core Data stack

2024년 4월 12일 금요일

Core Data stack

Manage and persist your app’s model layer.

 

(들어가보면 엄청나게 많지만 꼭 알아야 하는 핵심적인 아래의 몇 가지만 살펴봅니다.)


 

 

 

 


 

1. Persistent container ( NSPersistentContainer )

 

Container가 지정한 Model에 정의된 Entity들을 context를 활용하여 CRUD 한다.

let container = NSPersistentContainer.init(name: "Animals")

 

생성자에 전달한 name 은 컨테이너가 관리할 모델(.xcdatamodelld) 을 지정할 때 쓰인다.

 

 

 

 

2. Model ( NSManagedModel )

 

.xcdatamodeld 파일(이 클래스로 표현된 것)

 

스토리보드의 화면(main)을 코드로 표현할 수 있는 것과 비슷한 개념

 

UIStroyboard(name:)

 

 

 

NSManagedObject | Apple Developer Documentation

The base class that all Core Data model objects inherit from.

developer.apple.com

 

 

 

3. Context ( NSManagedObjectContext )

 

NSManagedObject 의 변경사항(수정사항)을 추적하는 클래스

→ CRUD 할 때 꼭 전달함

→ CRUD 완료 후 save() 를 호출해서 저장소에 반영함 **

NSManagedObject저장할 데이터의 기본 타입라고 생각하면 쉽다!

 

 

 

4. Store coordinator : 저장소를 다뤄주는 역할 

 

https://developer.apple.com/documentation/coredata/nspersistentstorecoordinator

 

NSPersistentStoreCoordinator | Apple Developer Documentation

An object that enables an app’s contexts and the underlying persistent stores to work together.

developer.apple.com

 

 

 

 

+ 참고 : Core Data Architecture

출처 : https://www.vadimbulavin.com/core-data-stack-swift-4/