派遣组通知?

时间:2018-07-29 16:55:58

标签: swift grand-central-dispatch

我在另一个应用程序中具有相同的代码,并且可以正常运行,现在在该应用程序中,通知组没有通知,并且未调用我的处理程序?由于代码相同,我无法弄清楚这两个应用之间的区别?

 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 个答案:

答案 0 :(得分:2)

将回调留在顶部是个好习惯

Internal Server Error: /create/
Traceback (most recent call last):
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/erichardson/env01/personalwebsite/projects/views.py", line 52, in create_projects
    return render(request, 'projects/forms.html', context)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/shortcuts.py", line 36, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/template/base.py", line 175, in render
    return self._render(context)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/template/base.py", line 167, in _render
    return self.nodelist.render(context)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/template/base.py", line 943, in render
    bit = node.render_annotated(context)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
    return self.render(context)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/template/base.py", line 993, in render
    output = self.filter_expression.resolve(context)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/template/base.py", line 676, in resolve
    obj = self.var.resolve(context)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/template/base.py", line 802, in resolve
    value = self._resolve_lookup(context)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/template/base.py", line 835, in _resolve_lookup
    current = current[bit]
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/forms/formsets.py", line 72, in __getitem__
    return self.forms[index]
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/forms/formsets.py", line 136, in forms
    for i in range(self.total_form_count())]
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/forms/formsets.py", line 110, in total_form_count
    return min(self.management_form.cleaned_data[TOTAL_FORM_COUNT], self.absolute_max)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/forms/formsets.py", line 92, in management_form
    code='missing_management_form',
django.core.exceptions.ValidationError: ['ManagementForm data is missing or has been tampered with']