         }
         
         let (statisticsQuery, startDate, endDate) = getQueryForPeriod(statisticsRange, qType: quantityType, statisticsOptions: statisticsOptions)
-        
+        let group = dispatch_group_create()
         statisticsQuery.initialResultsHandler = { query, results, error in
-            
             guard let statsCollection = results else {
                 // Perform proper error handling here
                 fatalError("*** An error occurred while calculating the statistics: \(error?.localizedDescription) ***")
             }
             var statisticsValues: [Double] = []
+            //cereate group
             statsCollection.enumerateStatisticsFromDate(startDate, toDate: endDate, withBlock: { (statistics, stop) in
+                //enter group
+                dispatch_group_enter(group)
                 var doubleMaxValue = 0.0
                 switch quantityType.aggregationStyle {
                     case .Discrete:

                            doubleMaxValue = maxValue.doubleValueForUnit(quantityType.defaultUnit!)
                        }
                }
                statisticsValues.append(round(doubleMaxValue))
            })
            completion(statisticsVlues: statisticsValues)
        }
        
        healthKitStore.executeQuery(statisticsQuery)

                case .Cumulative:

                    let predicate = HKQuery.predicateForSamplesWithStartDate(anchorDate, endDate: NSDate(), options: .StrictStartDate)
                    let query = HKStatisticsQuery(quantityType: quantityType, quantitySamplePredicate: predicate, options: .CumulativeSum) {
                        query, result, error in
                        guard error == nil else {
