WatchKit App仅返回一些HealthKit数据

时间:2015-09-23 14:20:38

标签: ios swift watchkit health-kit hksamplequery

我的IOS应用程序和WatchKit应用程序(Xcode 7 GM)都运行以下方法。 IOS应用程序返回所有正在运行的锻炼,但Watchkit应用程序仅返回前2个正在运行的锻炼。

有什么想法吗?

func readRunningWorkOuts(completion: (([AnyObject]!, NSError!) -> Void)!) {

    // 1. Predicate to read only running workouts
    let predicate =  HKQuery.predicateForWorkoutsWithWorkoutActivityType(HKWorkoutActivityType.Running)
    // 2. Order the workouts by date
    let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false)
    // 3. Create the query
    let sampleQuery = HKSampleQuery(sampleType: HKWorkoutType.workoutType(), predicate: predicate, limit: 0, sortDescriptors: [sortDescriptor])
        { (sampleQuery, results, error ) -> Void in

            if let queryError = error {
                print( "There was an error while reading the samples: \(queryError.localizedDescription)")
            }
            completion(results,error)
    }
    // 4. Execute the query
    healthKitStore.executeQuery(sampleQuery)

}

1 个答案:

答案 0 :(得分:1)

手表上的HealthKit仅提供最近7天的数据。之前创建的任何锻炼都只能在手机上进行。有关详细信息,请参阅HKHealthStore reference