Merge
Learn how merge joins one person’s samples from several sources without counting anything twice.
A person may wear two watches, and their phone may also hold readings in Health Connect or Apple Health. merge joins
all of them into one timeline.
Input
Pass a list of sources in priority order. Each source is a label, however your app names it (a watch address or
health-connect, say), and its samples. Your app reads Health Connect or Apple Health itself and passes those samples
as sources like any other.
Rules
- Where samples of one kind overlap, the earlier source wins, and within a source the earlier sample.
- A sample keeps only the time that no kept sample of its kind covers yet, and a step count shrinks with that share.
- A reading at one instant is dropped when a kept sample of its kind covers or repeats that instant.
Each merged sample keeps the label of the source it came from.
Errors
merge fails with ReversedSample when a sample ends before it starts, naming the source.