Skip to content

Instantly share code, notes, and snippets.

@torpedo87
Created March 24, 2019 08:35
Show Gist options
  • Save torpedo87/00e29735359f10e8ee80df458fdb25bd to your computer and use it in GitHub Desktop.
Save torpedo87/00e29735359f10e8ee80df458fdb25bd to your computer and use it in GitHub Desktop.
save and fetch

core data

  • Manage object graphs and object lifecycle, including persistence.
  • By default, Core Data uses a SQLite database as the persistent store
  • import 해야 사용가능

NSPersistentContainer

  • A container that encapsulates the Core Data stack in your application.
  • app delegate 에서 접근 가능

model

  • entity : class
  • attribute : property
  • relationship : class 간 관계
  • managed object : instance

NSManagedObject

  • core data 에 저장되어 있는 인스턴스로 타입을 아직 알 수 없다
  • A generic class that implements all the basic behavior required of a Core Data model object.
  • NSObject를 상속하므로 key, value 형태로 값을 읽을 수 있다
  • NSEntityDescription : A description of an entity in Core Data. 런타임에 타입을 유추 가능
  • entity(forEntityName:in:) : create a new managed object and insert it into the managed object context.

save 과정

  • NSManagedObjectContext 에 접근하기
  • entity(forEntityName:in:) 를 이용해 코어 데이터에 삽입
  • save 메소드를 사용해서 커밋하기

fetch 과정

  • NSManagedObjectContext 에 접근
  • NSFetchRequest 생성
  • fetch 메소드를 사용해 가져오기

NSManagedObjectContext

  • 프로젝트 생성시 use core data 선택하면 생성된다
  • NSPersistentContainer의 프로퍼티로 존재
  • an in-memory “scratchpad” for working with managed objects.
  • An object representing a single object space or scratch pad that you use to fetch, create, and save managed objects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment