public final class SeriesSample

  1. Object
  2. HealthSample
  3. SeriesSample

A run of measurements that share one record identity – a beat-to-beat heart-rate trace, a cadence series.

Why this type exists

The two platforms disagree about grouping. Health Connect’s HeartRateRecord is a single record containing many samples, and deleting it means deleting the record as a whole. HealthKit returns the same data as many independent samples with no grouping at all.

Flattening Health Connect into individual samples loses the identity you need in order to delete; inventing a series on iOS would claim a grouping that is not there. So the choice is yours: SampleQuery.setFlattenSeries(boolean) defaults to true, which gives both platforms plain QuantitySample objects and lets cross-platform code be identical. Turn it off when you need record identity – and note that iOS returns QuantitySample objects either way, not one-point series, because HealthKit stores each measurement separately and has no record to preserve. Test the type rather than casting.

Storage

Values are held in parallel primitive arrays rather than a list of objects. A month of continuous heart rate is tens of thousands of points, and boxing each one is real memory pressure on a phone.

Methods

public static SeriesSample create(HealthDataType type, long startMillis, long endMillis, long[] sampleStarts, long[] sampleEnds, double[] values, HealthUnit unit)Creates a series.
public int size()The number of measurements in this series.
public long getSampleStartMillis(int i)The start of measurement i, epoch millis UTC.
public long getSampleEndMillis(int i)The end of measurement i, epoch millis UTC.
public double getSampleValue(int i, HealthUnit in)Measurement i converted into in.
public HealthUnit getUnit()The unit every measurement in this series is expressed in.
public QuantitySample toQuantitySample(int i)Measurement i as a standalone QuantitySample.
public String toString()Returns a string representation of the object.

Inherited methods

Method details

create

public static SeriesSample create(HealthDataType type, long startMillis, long endMillis, long[] sampleStarts, long[] sampleEnds, double[] values, HealthUnit unit)

Creates a series. The three arrays must be the same length and are copied defensively.

The measurements must be in chronological order. Both platforms produce them that way, and readers rely on it: a reader asked for the newest N points of a long record takes them from the end rather than sorting half a million measurements to find out where they are.

Throws

IllegalArgumentException
if the arrays are null, differ in length, or unit is null.

size

public int size()
The number of measurements in this series.

getSampleStartMillis

public long getSampleStartMillis(int i)
The start of measurement i, epoch millis UTC.

getSampleEndMillis

public long getSampleEndMillis(int i)
The end of measurement i, epoch millis UTC. Equal to the start for an instantaneous measurement.

getSampleValue

public double getSampleValue(int i, HealthUnit in)
Measurement i converted into in.

Throws

IllegalArgumentException
if in measures a different dimension.

getUnit

public HealthUnit getUnit()
The unit every measurement in this series is expressed in.

toQuantitySample

public QuantitySample toQuantitySample(int i)

Measurement i as a standalone QuantitySample. Allocates, so prefer the indexed accessors when walking the whole series.

The returned sample inherits this series’ source, recording method and identifier – meaning several extracted samples share one identifier, since they came from one record.

toString

public String toString()
Returns a string representation of the object. In general, the toString method returns a string that “textually represents” this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + ‘@’ + Integer.toHexString(hashCode())