Skip to content

Instantly share code, notes, and snippets.

@torpedo87
Created March 25, 2019 06:09
Show Gist options
  • Save torpedo87/5578263a1eaf84e8d66bb11615dd77f3 to your computer and use it in GitHub Desktop.
Save torpedo87/5578263a1eaf84e8d66bb11615dd77f3 to your computer and use it in GitHub Desktop.
core data 모델링

attribute 데이터 타입

  • 이미지 같은 경우는 Binary data 타입으로 설정 가능. 이 때 allows extenrnal storage 등의 옵션 설정을 통해 메모리 이슈를 완화할 수 있다
  • String, Interger, Double, Boolean, Date, Binary data 등의 타입을 저장 가능
  • Transformable 타입 : custom 타입을 저장하고 싶을 때에는 먼저NSCoding 프로토콜을 준수하도록 만든 후에 Transformable 타입으로 설정해도 된다
  • NSCoding : A protocol that enables an object to be encoded and decoded for archiving and distribution.

entity 를 NSManagedObject 상속하는 클래스로 변환하기

  • entity를 좀 더 용이하게 다루기 위해서 하는 작업이다
  • key value 패턴으로 접근하는 방식은 위험하므로 managed object subclass 로 만들어서 접근한다
  • 컴파일을 하기 전에 Data Model inspector 에서 Code gen 설정
  • Editor -> Create NSManagedObject Subclass 들어가서 파일 생성 가능
  • @NSManaged attribute informs the Swift compiler that the backing store and implementation of a property will be provided at runtime instead of compile time. The normal pattern is for a property to be backed by an instance variable in memory. A property on a managed object is different: It’s backed by the managed object context, so the source of the data is not known at compile time.

NSManagedObjectContext

  • An object representing a single object space or scratch pad that you use to fetch, create, and save managed objects.
  • app delegate 에서 접근 가능한 NSManagedObjectContext를 얻어서 vc 에 주입해주는 것이 vc 에서 app delegate 에 접근하는 것보다 좋은 코드이다
  • 유효하지 않은 값을 저장 시도하면 에러 방출

data validation

  • 저장 시점에 그 값이 유효한지 판단해서 저장할 수 있는지 판단
  • model inspector 에서 코어데이터에 저장 가능한 값의 범위를 설정 가능하다
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment