Not sure this will work exactly - but this is the basic gist after gutting our model to an MVP. This assumes you've already requested permissions. You have to create a SRSensorReader with the sensor you want. Set a delegate for the reader's callbacks. Request the devices for the sensor. On the callback with devices, then you can request samples by creating an SRFetchRequest. Then when the data starts coming back, then you have to cast the sample into a type you're looking for and then extract the data from the sample. FWIW, the readers do not appear to be threadsafe - i.e., you can only be working with one reader at a time - don't attempt to read in parallel from different sensors.
class SensorKitDataExtractor : NSObject {
let reader: SRSensorReader
let fetchRequest = SRFetchRequest()
init() {
reader = SRSensorReader(sensor: .ambientLightSensor)
reader.delegate = self
reader.fetchDevices()
}
}
extension SensorKitDataExtractor : SRSensorReaderDelegate {
func sensorReader(_ reader: SRSensorReader, didFetch devices: [SRDevice]) {
fetchSamples(device: devices.first(where: { $0.model == self.queryRule.sensorType.device.rawValue }))
}
func sensorReader(_ reader: SRSensorReader, fetchDevicesDidFailWithError error: Error) {
print("Error fetching devices: \(error)")
}
func sensorReader(_ reader: SRSensorReader, fetching fetchRequest: SRFetchRequest, didFetchResult result: SRFetchResult<AnyObject>) -> Bool {
switch result.sampleObject {
case let lightSample as SRAmbientLightSample:
// do something with the data
default:
print("Unhandled sample type: \(result.sampleObject)")
return false
}
return true
}
func sensorReader(_ reader: SRSensorReader, didCompleteFetch fetchRequest: SRFetchRequest) {
print("Reader did complete fetch")
}
func sensorReader(_ reader: SRSensorReader, fetching fetchRequest: SRFetchRequest, failedWithError error: Error) {
print("Reader fetch failed: \(error)")
}
private func fetchSamples(device: SRDevice?) {
guard let device = device else {
print("No device found for this sensor")
return
}
fetchRequest.device = device
fetchRequest.from = SRAbsoluteTime.fromCFAbsoluteTime(_cf: fromDate)
fetchRequest.to = SRAbsoluteTime.fromCFAbsoluteTime(_cf: toDate)
reader.fetch(fetchRequest)
}
}