EddyVerbruggen / HealthKit

Cordova plugin for the iOS HealthKit framework
MIT License
168 stars 216 forks source link

querySampleType not calling its callback #118

Open tommykent1210 opened 3 years ago

tommykent1210 commented 3 years ago

I'm having some issues with the querySampleType function - everything seems to work EXCEPT that it doesn't actually callback and return its data...

   healthKitSupportedTypes: [
        'HKQuantityTypeIdentifierStepCount',
        'HKQuantityTypeIdentifierDistanceWalkingRunning',
        'HKQuantityTypeIdentifierHeartRate',
        'HKQuantityTypeIdentifierBodyTemperature',
      ],

    healthKitTypeUnits: {
        HKQuantityTypeIdentifierStepCount: 'count',
        HKQuantityTypeIdentifierDistanceWalkingRunning: 'm',
        HKQuantityTypeIdentifierHeartRate: 'count/min',
        HKQuantityTypeIdentifierBodyTemperature: 'degC',
    },

    healthKitLastSampleTime: null,

    doHealthKitLogic: function() {
        app.healthKitSupportedTypes.forEach(hkey => {
            var unit = app.healthKitTypeUnits[hkey];
            if(app.healthKitLastSampleTime == null) {
                // if there is no last date, get all data from the last month
                app.healthKitLastSampleTime = new Date(new Date().getTime() - 31 * 24 * 60 * 60 * 1000);
            }
            console.log(app.healthKitLastSampleTime);
            console.log(hkey);

            window.plugins.healthkit.querySampleType(
                {
                  'startDate': new Date(app.healthKitLastSampleTime), // three days ago
                  'endDate': new Date(), // now
                  'sampleType': hkey,
                  //'sampleType': 'HKQuantityTypeIdentifierStepCount', // anything in HealthKit/HKTypeIdentifiers.h
                  'unit': unit // make sure this is compatible with the sampleType
                },
                function(query) {
                    console.log([hkey, query]);
                    if(query.length > 0) {

                        app.queueAPIData(hkey, query);
                    } else {
                        console.log(hkey + " data length too short (0).");
                    }
                },
                function(msg) {
                    app.debugLog('Failed to get HealthKit data for: ' + element);
                    app.debugLog(msg);
                }
            );
        }); 

        app.debugLog('Updated most recent HealthKit Update time!');
        app.healthKitLastSampleTime = new Date();

        app.debugLog('Apple Healthkit Things!');
    },

If I put breakpoints at various points in the app in Safari and call app.doHealthKitLogic(), I can see that it calls console.log(hkey);. However, the callbacks never seem to get called. Whenever I put a breakpoint on them they are never called.

Any ideas?

Best wishes, Tom