Skip to content

Instantly share code, notes, and snippets.

@torpedo87
Last active March 30, 2019 05:54
Show Gist options
  • Save torpedo87/9c0bfb80a12c9fb48b2675748532dc3c to your computer and use it in GitHub Desktop.
Save torpedo87/9c0bfb80a12c9fb48b2675748532dc3c to your computer and use it in GitHub Desktop.
core data stack

core data stack

  • context(managed object) <—> coordinator(persistent store) <—-> SQLite
  • The managed object context keeps track of its managed objects and all the changes you make to them
  • NSPersistentContainer : A container that encapsulates the Core Data stack in your application. 스택 세팅을 위한 helper class

Xcode 도움없이 core data 세팅하기

NSManagedObjectModel

  • A programmatic representation of the .xcdatamodeld file describing your objects.
  • database schema 역할

NSPersistentStore

  • The abstract base class for all Core Data persistent stores.
  • atomic store : 데이터를 읽고 쓰기 전에 deserialize 해서 메모리에 로드해야함 (XML, Binary, InMemory)
  • non atomic store : 바로 메모리에 로드 가능 (SQLite)
  • NSIncrementalStore : An abstract superclass defining the API through which Core Data communicates with a store. 이걸 상속해서 커스텀 스토어 생성 가능

NSPersistentStoreCoordinator

  • A coordinator that uses the model to help contexts and persistent stores communicate.

NSManagedObjectContext

  • An object space that you use to manipulate and track changes to managed objects.
  • managed object의 life cycle 관리
  • A context is not thread-safe. The same goes for a managed object: You can only interact with contexts and managed objects on the same thread in which they were created.

NSPersistentContainer

  • A container that encapsulates the Core Data stack in your application.
  • 위의 4가지 스택을 모두 포함
  • context 만 public 으로 접근 가능

스택 세팅하기

  • class CoreDataStack 생성해서 스택을 세팅한다
  • 모델 파일을 생성할 때 코어데이터 템플릿을 생성하고 확장자를 swift가 아닌 xcdatamodeld 로 해야한다
  • entity 간의 관계는 relationship 에서 설정가능
  • Editor -> Create NSManagedObject subclass 를 선택하여 각 entity의 모델파일 생성
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment