Skip to content

Instantly share code, notes, and snippets.

@torpedo87
Created March 27, 2019 07:32
Show Gist options
  • Save torpedo87/beae0f9da533a57aad464dc22cfc7b72 to your computer and use it in GitHub Desktop.
Save torpedo87/beae0f9da533a57aad464dc22cfc7b72 to your computer and use it in GitHub Desktop.
fetching from core data

fetching 기술

NSFetchRequest

  • A description of search criteria used to retrieve data from a persistent store.
  • 같은 request를 여러번 호출해야 할 경우에는 xcdatamodeld 파일에서 request를 생성해서 사용할 수 있다. 단 이렇게 하면 정렬 순서를 설정할 수 없는 단점이 있다
  • resultType : The result type of the fetch request.

NSFetchRequestResultType

  • managedObjectResultType
  • countResultType
  • dictionaryResultType

갯수를 구하는 효율적인 방법

  • 데이터들을 모두 가져온 후 갯수를 세는 것보다 바로 갯수를 얻는 방법이 더 효과적이다
  • resultType 을 countResultType 로 하는 방법
  • NSManagedObjectContext의 fetch 메소드 대신에 count 메소드를 호출하는 방법
  • 갯수를 구하는 것 이외에도 데이터들을 이용해 더하기 등의 계산을 할 때에도 데이터를 일단 모두 가져온 후 이를 이용해 계산을 하는 것보다 NSExpressionDescription를 사용해서 request 생성시 미리 설정을 해놓아서 그 결과값만 가져올 수 있다 Use NSFetchRequest’s dictionary result type to efficiently compute and return averages, sums and other common calculations from SQLite.

NSPredicate

  • A definition of logical conditions used to constrain a search either for a fetch or for in-memory filtering.

데이터 불러오는 양 제한하기

  • fetchBatchSize, fetchLimit, fetchOffset 설정하기
  • faulting : A fault is a placeholder object representing a managed object that hasn’t yet been fully brought into memory
  • predicate 사용하기

데이터 정렬

  • NSSortDescriptor : An immutable description of how to order a collection of objects based on a property common to all the objects.
  • 메모리가 아닌 SQLite 상에서 정렬하므로 성능상 효율적

NSAsynchronousFetchRequest

  • A fetch request that retrieves results asynchronously and supports progress notification.
  • NSPersistentStoreRequest 를 상속한다

일괄 업데이트

  • 예) 모든 메일을 모두 읽음으로 표시하기
  • 메모리에 fetch 하지 않고도 데이터를 업데이트하는 기술
  • NSBatchUpdateRequest : A request to Core Data to do a batch update of data in a persistent store without loading any data into memory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment