healthkit query

My HealthKit query is not working.
I am trying to get the steps count and I am confused on which part of my code is in-correct.
When I build the app, it either throws "updateStepsCount is not a recognised objc method" or "Unhandled promise rejection".
Where am I going wrong here!!!

Controller.swift:
Code Block
@objc
func updateStepsCount(_ statisticsCollection: HKStatisticsCollection, _ resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping (RCTPromiseRejectBlock) -> Void) {
let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
let startDate = Calendar.current.date(byAdding: .day, value: -7, to: Date())
let anchorDate = Date.mondayAt12AM()
let daily = DateComponents(day: 1)
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictStartDate)
let query = HKStatisticsCollectionQuery(quantityType: stepType, quantitySamplePredicate: predicate, options: .cumulativeSum, anchorDate: anchorDate, intervalComponents: daily)
healthStore.execute(query)
struct Step {
let id = UUID()
var count: Int?
var date: Date?
}
let endDate = Date()
statisticsCollection.enumerateStatistics(from: startDate!, to: endDate) { (statistics, stop) in
let count = statistics.sumQuantity()?.doubleValue(for: .count())
let steps = Int(count ?? 0)
var stepCount = [Step]()
var tempStepCount = Step(count: steps, date: Date())
tempStepCount.count = steps
tempStepCount.date = startDate
stepCount.append(tempStepCount)
}
resolve(Step())
}

Controller.m
Code Block
RCT_EXTERN_METHOD(updateStepsCount: (HKStatisticsCollection)someStatisticsCollection
resolve: (RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)

JS side:
Code Block
clickHandler = async() => {
const login = HealthkitController.updateStepsCount()
.then(result => console.warn(result));
}

Have you tried removing @escaping for RCTPromiseRejectBlock ?

Code Block
@objc
func updateStepsCount(_
resolve: @escaping RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
checkNotificationEnabled { enabled in
resolve(enabled)
}
}

// Or maybe in your case
Code Block
@objc
func updateStepsCount(_ statisticsCollection: HKStatisticsCollection, _ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: (RCTPromiseRejectBlock) -> Void) {

See:
https://stackoverflow.com/questions/59732722/swift-how-can-i-call-my-function-inside-completion-body

This may be of interest as general discussion:
https://forums.swift.org/t/implicit-escaping-of-closures-via-objective-c/12025

You should better clarify that you are writing a native method interfaced to React Native.

I really do not want to go deep into React Native, with 2 reasons:
  • It is not a framework of Apple's.

  • I do not like it

You should better find an appropriate place to ask something about React Native.


I just want to correct terrible mis-directions...

which part of my code is in-correct

Your code has two big flaws:
  • JS.side and Swift code are inconsistent

Your JS code calls updateStepsCount() with no arguments.
But in your Swift code has a parameter before resolve. It represents that the JS side should pass one parameter.
  • You call execute(query), but there is no code receive the result of query

The result would never go into statisticsCollection without writing an explicit code to do so.
But this problem should be resolved in another thread of yours.


Issue #1 JS.side and Swift code are inconsistent


I think you cannot create an instance of HKStatisticsCollection, which means you cannot pass a parameter to Swift method.

Remove the first parameter of updateStepsCount:
Code Block
@objc
func updateStepsCount(_ resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock) {
// Find the right code in another thread of yours...
}

Controller.m
Code Block
RCT_EXTERN_METHOD(updateStepsCount: (RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject)


healthkit query
 
 
Q