Device Data
In MyDataHelps, participants may contribute data collected by wearables and other devices.
Because device data is a powerful tool available to MyDataHelps projects, we strongly recommend reading about device data concepts.
Query Device Data Points
Construct a DeviceDataQuery
to specify filtering criteria, and execute the query using ParticipantSession.queryDeviceData
. This query returns a DeviceDataResultPage
containing DeviceDataPoint
values.
At minimum, DeviceDataQuery requires a namespace
value to filter by. Use known values of DeviceDataNamespace
such as .fitbit
or .project
. Your query may also want to filter by type
values retrieve specific types or categories of device data. See device data concepts for more information about namespaces and types, and project data collection settings for techniques to determine specific namespace/type values to use in your query.
Consult the programming guide for best practices on working with queries and paged results in MyDataHelpsKit.
SDK Reference
Examples
Working with project device data:
let criteria = DeviceDataQuery(namespace: .project, observedAfter: Date.now.addingTimeInterval(-86_400))
let result = try await session.queryDeviceData(criteria)
for dataPoint in result.deviceDataPoints {
print("\(dataPoint.type): \(dataPoint.value)")
}
Retrieving the most recent device data point of a given type:
let criteria = DeviceDataQuery(namespace: .appleHealth, types: Set(["RestingHeartRate"]), limit: 1)
if let latestHR = try await session.queryDeviceData(criteria).deviceDataPoints.first {
print("Your latest RHR: \(latestHR.value) on \(latestHR.observationDate?.formatted())")
}
In the MyDataHelpsKit example app, see:
Persist Device Data Points
Use DeviceDataPointPersistModel
and ParticipantSession.persistDeviceData
to create new and/or update existing device data points.
Each device data point is uniquely identified by a combination of its properties, called a natural key, as identified in DeviceDataPointPersistModel
. Data points are always persisted with the project
namespace.
SDK Reference
Examples
let source = DeviceDataPointSource(identifier: "BPCuff1", properties: ["Model": "BP130X", "Serial": "12345"])
let dataPoint = DeviceDataPointPersistModel(identifier: UUID().uuidString, type: "HeartRate", value: "73", units: "count/min", properties: ["PostalCode": "48104", "Sequence": "32"], source: source, startDate: nil, observationDate: .now)
do {
try await session.persistDeviceData([dataPoint])
print("Data point updated.")
} catch {
print("Error persisting data point: \(MyDataHelpsError(error))")
}
In the MyDataHelpsKit example app, see: