菜鸟教程小白 发表于 2022-12-11 20:00:07

ios - HKSampleQuery 即使有


                                            <p><p>我正在尝试获取过去一个月的所有心率样本,并从中提取时间和值。</p>

<p>到目前为止,我有以下方法:</p>

<pre><code>func getThisMonthsHeartRates() {
    print(&#34;func called&#34;)
    let heartRateUnit:HKUnit = HKUnit(from: &#34;count/min&#34;)
    let heartRateType:HKQuantityType = HKQuantityType.quantityType(forIdentifier: .heartRate)!

    //predicate
    let startDate = Date()
    let endDate = Date() - 1.month
    let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [])

    //descriptor
    let sortDescriptors = [
      NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
    ]

    let heartRateQuery = HKSampleQuery(sampleType: heartRateType,
                                 predicate: predicate,
                                 limit: Int(HKObjectQueryNoLimit),
                                 sortDescriptors: sortDescriptors)
    { (query:HKSampleQuery, results:?, error:Error?) -&gt; Void in

      guard error == nil else { print(&#34;error&#34;); return }
      print(&#34;results&#34;)
      print(results!)

      for result in results! {
            guard let currData:HKQuantitySample = result as? HKQuantitySample else { return }

            print(&#34;Heart Rate: \(currData.quantity.doubleValue(for: heartRateUnit))&#34;)
            print(&#34;quantityType: \(currData.quantityType)&#34;)
            print(&#34;Start Date: \(currData.startDate)&#34;)
            print(&#34;End Date: \(currData.endDate)&#34;)
            print(&#34;Metadata: \(String(describing: currData.metadata))&#34;)
            print(&#34;UUID: \(currData.uuid)&#34;)
            print(&#34;Source: \(currData.sourceRevision)&#34;)
            print(&#34;Device: \(String(describing: currData.device))&#34;)
            print(&#34;---------------------------------\n&#34;)
      }

    }//eo-query
    healthStore.execute(heartRateQuery)

}//eom
</code></pre>

<p>但是,结果将始终返回一个空数组,即使我的设备上有样本!真的很好奇这是怎么回事以及如何解决它。我完全不知所措。</p>

<p>谢谢</p>

<p><strong>更新</strong></p>

<p>在查询执行前和执行期间记录查询后,控制台会这样说:</p>

<pre><code>&lt;HKSampleQuery:0x1c4117610 inactive&gt;
</code></pre>

<p>还有</p>

<pre><code>&lt;HKSampleQuery:0x1c4117610 deactivated&gt;
</code></pre>

<p>我不知道这意味着什么,我也无法在网上找到任何关于它的信息。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>问题可能是您已请求授权以编写 <code>.heartRate</code> 样本类型,但未同时读取它们。在这种情况下,您在执行查询时不会收到错误,但是 samples 数组将为空。</p>

<p>我遇到了同样的问题,因为我是这样请求授权的:</p>

<pre><code>healthStore.requestAuthorization(toShare: types, read: nil) {}
</code></pre>

<p>相反,您需要指定要读取的类型,即使它们已经在 <code>types</code> 集中。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - HKSampleQuery 即使有,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/49729905/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/49729905/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - HKSampleQuery 即使有