티스토리 뷰

HealthKit에서는 데이터를 처리할 때 각각 용도에 맞게 정의된 타입으로 처리하는데요.

이에 대해 정리해보았습니다.

 

DataTypes (표현 방식 분류)


건강 데이터의 표현 방식을 식별하는데 사용됩니다.

let bloodType = HKObjectType.characteristicType(forIdentifier: .bloodType)

let caloriesConsumed = HKObjectType.quantityType(forIdentifier: .dietaryEnergyConsumed)

let sleepAnalysis = HKObjectType.categoryType(forIdentifier: .sleepAnalysis)

이처럼 각 데이터를 표현하기 적절한 type과 구체적인 데이터 종류인 Identifier를 통해 쿼리를 작성할 수 있습니다.

 

HKObjectType

HK~Type들의 슈퍼클래스
  • Property: uuid, sourceRevision, device, metadata…
모든 HKDataType의 슈퍼클래스 이므로 모든 HealthKit 데이터는 이 type의 프로퍼티를 갖습니다.
즉 uuid, source, device, metadata 등이 모두 포함되어 있습니다.

HKSampleType

HKSample들의 추상클래스
  • Property: startDate, endDate, 알 수 없는 기간 여부(hasUndeterminedDuration) 등
HealthKit에서 측정된 각각의 데이터를 Sample이라고 부릅니다.
각각의 데이터는 startDate, endDate등 날짜 정보를 갖습니다.

HKQuantityType

숫자값으로 저장 가능한 데이터
  • 이 타입 객체는 quantity값을 가짐
예를 들어 걸음수와 같이 정량적인 데이터는 HKQuantityType으로 정의되고 quantity라는 프로퍼티로 값에 접근할 수 있습니다.

HKCategoryType

유한한 카테고리로 나눌 수 있는 데이터
  • 이 타입 객체는 value값을 가짐
생리 주기같은 기간 정보는 측정가능한 값이 아닌 분류 형태로 저장됩니다.
value 프로퍼티는 각 기간의 종류를 나타내는 enum의 rawValue 값입니다.

HKWorkoutType

물리적 활동을 표현하는 데이터
  • 이 타입 객체는 HKQuntity, HKWorkoutActivityType, HKWorkoutEvent 등을 가짐
운동 정보 같은 경우 기본적으로 HKQuntity 정보와 함께 HKWorkoutActivityType(운동 종류), HKWorkoutEvent(이벤트 시작, 일시정지 등등..)에 필요한 정보 등을 갖습니다.

 

정리

데이터(Sample)는 ObjectType + SampleType + (QuntityType or CategoryType or WorkoutType) 속성으로 구성되며, 각 Type에 대한 프로퍼티를 갖습니다.
또한 Quntity/Category, Workout은 명확한 상속관계가 있지 않아, WorkoutType안에서 QuntityType를 가질 수 있습니다.

 

Samples (실제 데이터)


DataType은 실제 프로퍼티를 갖는 것은 아니고 단순 식별용으로, 쿼리를 작성할 때 사용됩니다.

실제 데이터는 Sample을 이용해서 접근할 수 있습니다.

쿼리를 통해 조회한 데이터는 Sample 형태로 받아와지고, 각 프로퍼티에 접근할 수 있습니다.

HKQuantitySample

  • HKQuntityType: HKQuntityType
  • quantity: HKQuantity
  • count: Int (quntity 수)
  • startDate: Date
  • endDate: Date

HKCategorySample

  • HKCategoryType: HKCateotryType
  • value: Int (enum rawValue)
  • startDate: Date
  • endDate: Date
더보기

그 외

HKDiscreteQuantitySample

이산형 데이터
  • averageQuantity: HKQuantity
  • maximumQuantity: HKQuantity
  • minimumQuantity: HKQuantity
  • mostRecentQuantity: HKQuantity
  • mostRecentQuantityDateInterval: DateInterval

HKCumulativeQuantitySample

누적 수량
  • sumQuantity: HKQuantity

HKCorrelation

상관 데이터
  • correlationType: HKCorrelationType
  • objects: Set<HKSample>

 

Util


HKQuntity

  • Unit: HKUnit
  • value: double

HKUnit

  • 인치, 센치 등 quantity의 단위를 나타냅니다.

 

Identifier (건강 데이터 종류)


실제로 HealthKit이 수집하고 있는 구체적인 데이터 종류입니다.

종류가 많아 예시만 들겠습니다.

HKQuantityTypeIdentifier

  • heartRate: 맥박수
  • stepCount: 걸음 수
  • height: 키

HKCategoryTypeIdentifier

  • sleepAnalysis: 수면 정보
  • mindfulSession: 명상 정보
  • menstrualFlow: 생리 주기

사용

let heartRate = HKObjectType.quantityType(forIdentifier: .heartRate)
let sleepAnalysis = HKObjectType.categoryType(forIdentifier: .sleepAnalysis)

위에서 본 것 같이 HKObjectType에서 해당하는 메소드를 선택하고 forIdentifier에 해당 식별자를 적어주면 됩니다.

 

 

마치며


대략적으로 설명해보자면

Input: Identifier+Type --Query--> Output: Sample

형태입니다.

전체적인 구조는 이정도이고 데이터 종류나 형은 훨씬 많아서 필요할 때마다 찾아서 구현하려고합니다. 

 

 

감사합니다!

 

ref.

공식문서

https://velog.io/@sun02/HealthKit-%EB%8D%B0%EC%9D%B4%ED%84%B0%ED%83%80%EC%9E%85%EB%93%A4%EC%9D%98-%EC%A0%9C%EA%B3%B5-%ED%95%AD%EB%AA%A9

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함