Swift:似乎无法从Healthkit查询中返回值

时间:2019-01-22 11:30:55

标签: ios swift health-kit hkhealthstore

我正在尝试设置单独的功能,我将使用该功能来检索特定的Healthkit数据,例如步行心率等。该函数本身可以工作并且正在检索/打印数据,但是我试图将数据返回到ViewController(最初从中调用该函数的地方),但是没有获取所检索到的数据,而是一直返回0和im不确定原因。

到目前为止,我已经尝试运行代码而不返回任何值,而只是打印它们,结果是相同的。从函数本身打印的值正确,而从视图控制器打印的值始终为0。

视图控制器:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    let walkingHR = HealthKitManager.retriveHeathKitData(dataType: "walkingHeartRate")


    print(String(format: "%.0f", walkingHR))
    setUpLabels(type: "walkingHeartRate", samples: walkingHR)

}

HealthKitManager

静态函数retriveHeathKitData(dataType:String)-> Double {

        var comp: DateComponents = Calendar.current.dateComponents([.year, .month, .day], from: Date())
        comp.timeZone = TimeZone(abbreviation: "UTC")!
        let truncated = Calendar.current.date(from: comp)!

        var  beats: Double? = 0.0001

        var typeHeart = HKQuantityType.quantityType(forIdentifier: .restingHeartRate)

        switch dataType{

        case "walkingHeartRate":
            typeHeart = HKQuantityType.quantityType(forIdentifier: .walkingHeartRateAverage)
            break
        case "restingHeartRate":
            typeHeart = HKQuantityType.quantityType(forIdentifier: .restingHeartRate)
            break
        case "heartRateVariability":
            typeHeart = HKQuantityType.quantityType(forIdentifier: .heartRateVariabilitySDNN)
            break

        default:

            print("Get Heart Rate Data Type not defined", "dataType  switch 1 from retriveHeathKitData")
        }


        let startDate = truncated

        let endDate = Date()
        let predicate: NSPredicate? = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: HKQueryOptions.strictEndDate)

        let squery = HKStatisticsQuery(quantityType: typeHeart!, quantitySamplePredicate: predicate, options: .discreteAverage, completionHandler: {(query: HKStatisticsQuery,result: HKStatistics?, error: Error?) -> Void in
            DispatchQueue.main.async(execute: {() -> Void in



                var type: String = ""

                switch dataType{

                case "walkingHeartRate":
                    let quantity: HKQuantity? = result?.averageQuantity()
                    beats = quantity?.doubleValue(for: HKUnit.count().unitDivided(by: HKUnit.minute()))
                    type = "walkingHR"
                    break
                case "restingHeartRate":
                    let quantity: HKQuantity? = result?.averageQuantity()
                    beats = quantity?.doubleValue(for: HKUnit.count().unitDivided(by: HKUnit.minute()))
                    type = "restingHR"
                    break
                case "heartRateVariability":
                    let quantity: HKQuantity? = result?.averageQuantity()
                    beats = quantity?.doubleValue(for: HKUnit.secondUnit(with: .milli))
                    type = "variabilityHR"
                    break
                default:
                    print("Get Heart Rate Data Type not defined", "dataType  switch 2 from retriveHeathKitData")
                    beats = 0.00
                }


                print("got:  \(String(format: "%.f", beats!))")
                //print(dataType)

            })
        })
        healthKitStore.execute(squery)

    return(beats!)

    }

实际结果如下

视图控制器得到:0 *保健套件经理*获得:89

我期望的是,视图控制器和运行状况工具包管理器将具有相同的输出,但是由于某种原因,情况并非如此。

也可能是该函数本身设置不正确,但是很快就不熟悉了。

P.S。预先感谢

0 个答案:

没有答案