代码之家  ›  专栏  ›  技术社区  ›  GarySabo

调度组没有通知?[复制品]

  •  -1
  • GarySabo  · 技术社区  · 7 年前

    这个问题已经有了答案:

    我在另一个应用程序中有相同的代码,它可以正常工作,现在在这个应用程序中,调度组没有通知,我的处理程序没有被调用?我不知道这两个应用程序之间的区别,因为代码是相同的?

     func getHeartRateMaxFromWorkouts(workouts: [HKWorkout], handler: @escaping (careerMaxHeartRatePerWorkoutAsCustomHistoricalSample, careerAverageHeartRatePerWorkoutAsCustomHistoricalSample) -> Void) {
    
            let workoutsReversed = workouts.reversed()
            guard let heartRateType:HKQuantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate) else { return }
            let heartRateUnit:HKUnit = HKUnit(from: "count/min")
            var heartRateMaxArrayAsCustomHistoricalSample = [CustomHistoricalSample]()
            var heartRateAvgArrayAsCustomHistoricalSample = [CustomHistoricalSample]()
    
            //DispatchGroup needed since making async call per workout and need notified when all calls are done
            let dispatchGroup = DispatchGroup()
    
            for workout in workoutsReversed {
    
                //predicate
                let startDate = workout.startDate
                let endDate = workout.endDate
    
                let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictStartDate)
    
                //descriptor
                let sortDescriptors = [
                    NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: true) //Changed this to false so that HRR and MCS would calculate, always check this if not getting these values
                ]
                dispatchGroup.enter()
                let heartRateQuery = HKSampleQuery(sampleType: heartRateType,
                                                   predicate: predicate,
                                                   limit: (HKObjectQueryNoLimit),
                                                   sortDescriptors: sortDescriptors)
                    { (query:HKSampleQuery, results:[HKSample]?, error:Error?) -> Void in
    
                        guard error == nil else { print("get heart rate error"); return }
    
                        guard let unwrappedResults = results as? [HKQuantitySample] else { print("get heart rate error"); return}
    
                        let heartRatesAsDouble = unwrappedResults.map {$0.quantity.doubleValue(for: heartRateUnit)}
    
                        guard let max = heartRatesAsDouble.max() else { return }
    
                        let maxAsCustomHistoricalSample = CustomHistoricalSample(value: max, date: workout.startDate)
                        heartRateMaxArrayAsCustomHistoricalSample.append(maxAsCustomHistoricalSample)
    
                        let average = heartRatesAsDouble.average
                        let averageAsCustomHistoricalSample = CustomHistoricalSample(value: average, date: workout.startDate)
                        heartRateAvgArrayAsCustomHistoricalSample.append(averageAsCustomHistoricalSample)
    
                        dispatchGroup.leave()
    
                    }
                healthStore.execute(heartRateQuery)
    
            } //End of for workout loop
    
            dispatchGroup.notify(queue: .main) {
    
                //Need to sort by date since the dates come back jumbled 
                let sortedReversedHeartRateMaxArrayAsCustomHistoricalSampple = heartRateMaxArrayAsCustomHistoricalSample.sorted { $0.date > $1.date }.reversed() as [CustomHistoricalSample]
    
                 let sortedReversedHeartRateAverageArrayAsCustomHistoricalSampple = heartRateAvgArrayAsCustomHistoricalSample.sorted { $0.date > $1.date }.reversed() as [CustomHistoricalSample]
                print("handler called = \(sortedReversedHeartRateMaxArrayAsCustomHistoricalSampple.count)")
    
                handler(sortedReversedHeartRateMaxArrayAsCustomHistoricalSampple, sortedReversedHeartRateAverageArrayAsCustomHistoricalSampple)
            }
    
    
    
        } //End getHeartRateMaxFromWorkouts
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Shehata Gamal    7 年前

    guard error == nil else { print("get heart rate error");  dispatchGroup.leave() return ; }
    
    guard let unwrappedResults = results as? [HKQuantitySample] else { print("get heart rate error"); dispatchGroup.leave(); return}
    
    let heartRatesAsDouble = unwrappedResults.map {$0.quantity.doubleValue(for: heartRateUnit)}
    
    guard let max = heartRatesAsDouble.max() else { dispatchGroup.leave();  return }
    
    let maxAsCustomHistoricalSample = CustomHistoricalSample(value: max, date: workout.startDate)
    
    heartRateMaxArrayAsCustomHistoricalSample.append(maxAsCustomHistoricalSample)
    
    let average = heartRatesAsDouble.average
    
    let averageAsCustomHistoricalSample = CustomHistoricalSample(value: average, date: workout.startDate)
    
    dispatchGroup.leave()
    
    heartRateAvgArrayAsCustomHistoricalSample.append(averageAsCustomHistoricalSample)