Swift WatchKit HealthKit-以两种不同方式访问时,时间跨度的获取步长范围不同

Swift WatchKit HealthKit-以两种不同方式访问时,时间跨度的获取步长范围不同,swift,xcode,watchkit,healthkit,watchos,Swift,Xcode,Watchkit,Healthkit,Watchos,我正在开发一款apple watch应用程序,它使用人的步调 目前有两种方法使用这些步骤: 从HealthKit检索当天的步骤,以查看用户是否达到10000个步骤 当按下按钮时,用户从上次按下按钮到现在的步骤 当用户第一次按下sync按钮时,它只会从一天开始执行步骤发生这种情况时,与步长值不一致。 从今天开始的总步骤和从一天开始的用户步骤返回不同的值。我为此使用了两个不同的函数,但在本例中它们应该返回相同的结果 例如: “我的手表”活动应用程序中今天步骤的步骤是:2489 我的苹果手机的健康摘

我正在开发一款apple watch应用程序,它使用人的步调

目前有两种方法使用这些步骤:

  • 从HealthKit检索当天的步骤,以查看用户是否达到10000个步骤
  • 当按下按钮时,用户从上次按下按钮到现在的步骤
  • 当用户第一次按下sync按钮时,它只会从一天开始执行步骤发生这种情况时,与步长值不一致。

    从今天开始的总步骤和从一天开始的用户步骤返回不同的值。我为此使用了两个不同的函数,但在本例中它们应该返回相同的结果

    例如:

    • “我的手表”活动应用程序中今天步骤的步骤是:2489
    • 我的苹果手机的健康摘要中关于今天步骤的步骤是: 2693
    • 当代码返回一天的总步数时,它返回:2693
    • 当代码从一天开始到现在都有步骤时,它 报税表:4113
    作为参考,返回4113步的函数参数的起始日期为
    2021-04-01 13:30:00+0000
    ,结束日期为
    2021-04-02 07:07:33+0000
    ,函数在下午5:37执行。获取天数步长的函数的数据参数为
    2021-04-01 13:30:00+0000
    。我的时区是格林尼治标准时间+10:30,所以我认为这不是时区的问题

    为什么这些值之间存在如此大的差异?

    我有一个
    HealthKitManager
    类,它处理
    HealthKit
    数据的返回。 其中有两个函数返回一天的总步数,以及上次同步到现在之间的步数

    代码如下:

    class HealthKitManager {
    let storage = HKHealthStore()
    static var dailyStepGoal = 10000
    
    func recentSteps(_ date : Date, completion: @escaping (Double, NSError?) -> () )
    {
        print("recentSteps start: ")
        print(date)
        print("recentSteps end: ")
        print(Date())
        // The type of data we are requesting (this is redundant and could probably be an enumeration
        let type = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)
        
        let predicate = HKQuery.predicateForSamples(withStart: date, end: Date(), options: HKQueryOptions())
        
        // The actual HealthKit Query which will fetch all of the steps and sub them up for us.
        let query = HKSampleQuery(sampleType: type!, predicate: predicate, limit: 0, sortDescriptors: nil) { query, results, error in
            var steps: Double = 0
            
            if results?.count > 0
            {
                for result in results as! [HKQuantitySample]
                {
                    steps += result.quantity.doubleValue(for: HKUnit.count())
                }
            }
            
            completion(steps, error as NSError?)
        }
        
        storage.execute(query)
    }
    
    
    func TodayTotalSteps(_ completion: @escaping (_ stepRetrieved: Double) -> Void) {
        
        let type = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount) // The type of data we are requesting
        
        let calendar = Calendar.current
        var interval = DateComponents()
        interval.day = 1
        
        var anchorComponents = (calendar as NSCalendar).components([.day , .month , .year], from: Date())
        anchorComponents.hour = 0
        let anchorDate = calendar.date(from: anchorComponents)
        
        print("anchorDate: ")
        print(anchorDate)
        
        let stepsQuery = HKStatisticsCollectionQuery(quantityType: type!, quantitySamplePredicate: nil, options: .cumulativeSum, anchorDate: anchorDate!, intervalComponents: interval)
        
        stepsQuery.initialResultsHandler = {query, results, error in
            let endDate = Date()
            
            var steps = 0.0
            let startDate = (calendar as NSCalendar).date(byAdding: .day, value: 0, to: endDate, options: [])
            if let myResults = results{  myResults.enumerateStatistics(from: startDate!, to: endDate) { statistics, stop in
                if let quantity = statistics.sumQuantity(){
                    //let date = statistics.startDate
                    steps = quantity.doubleValue(for: HKUnit.count())
                    // print("\(date): steps = \(steps)")
                }
                
                completion(steps)
                }
            } else {
                
                completion(steps)
            }
        }
        storage.execute(stepsQuery)
    }
    }
    
    我认为最近的步骤功能就是问题所在。该函数返回从上次同步日期到现在的步骤。它由一个按钮触发:

            let syncDate = Date()
            HKM.recentSteps(User.Player.lastSyncDate) { steps, error in
                let stepsToGive = Int(steps)
                User.Player.pokemon[0].walk(steps: stepsToGive)
                print("steps " + String(steps))
                User.Save() //this needs to be here as well as below for some reason
            }
            
            User.Player.UpdateWalkAchievements()
            User.Player.lastSyncDate = syncDate
            
            User.Save()
            willActivate()
        }
    
    Would anyone know why there is a discrepancy between the steps returned from the two functions?