                     }
                     
                 } else if type == HKCategoryTypeIdentifierSleepAnalysis {
+                    dispatch_group_leave(group)//leave main group
                     //HealthManager.sharedManager.getStatisticsForSleepInPeriod(period)
                 } else {
                     HealthManager.sharedManager.getStatisticForPeriod(period, forType: type) { (statisticsVlues) in
-                        let statisticsVluesData = NSKeyedArchiver.archivedDataWithRootObject(statisticsVlues)
-                        Defaults[key] = statisticsVluesData
+                        let statisticsValuesData = NSKeyedArchiver.archivedDataWithRootObject(statisticsVlues)
+                        Defaults[key] = statisticsValuesData
                         dispatch_group_leave(group)//leave main group
                     }
                 }

        }
    }
    
    public func getSamplesForYear (type: String, completion: (numberOfDaysWithSamplesForEachMonth: [Double]) -> Void) {
        guard let quantityType = HKObjectType.quantityTypeForIdentifier(type) else {
            fatalError("*** Unable to create a \(type) count type ***")
        }
        let group = dispatch_group_create()
        let calendar = NSCalendar.currentCalendar()
        let startDate = calendar.dateByAddingUnit(.Month, value: -11, toDate: NSDate(), options: [])!        
        var numberOfDaysWithSamples:[Double] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        let smaplePredicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: NSDate(), options: .None)//create predicate for year
        dispatch_group_enter(group)
        
        let uniqDayDateFormatter = NSDateFormatter()
        uniqDayDateFormatter.dateFormat = "yyyy-MM-dd"
        
        let uniqMonthDateFormatter = NSDateFormatter()
        uniqMonthDateFormatter.dateFormat = "yyyy-MM"
        
        let sampleQuery = HKSampleQuery.init(sampleType: quantityType, predicate: smaplePredicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil, resultsHandler: { (query, results, error) in
            if let result = results {//if we have samples for the month period
//                print("END QUERY EXEQUTE - \(NSDate())")
                var seenDate:[String:Bool] = [:]
                let filteredResults = result.filter({ (sample) -> Bool in
                    let sampleDateKey = uniqDayDateFormatter.stringFromDate(sample.startDate)
                    let retValue:Bool? = seenDate[sampleDateKey]
                    seenDate[sampleDateKey] = false
                    if retValue != nil {
                        return retValue!
                    } else {
                        return true
                    }
                })
                for monthInedx in 0...11 {//iterate from 0 to 10 that equals 12 monthes
                    dispatch_group_enter(group)
                    let incrementIndex = monthInedx + 1
                    let predicateStartDate = calendar.dateByAddingUnit(.Month, value: incrementIndex, toDate: startDate, options: [])! //increase start date each iteration with one month
                    //create start of month date
                    let components = calendar.components([.Year, .Month], fromDate: predicateStartDate)
                    let mothResults = filteredResults.filter({ (sample) -> Bool in
                        let sampleDate = sample.startDate
                        let sampleComponents = calendar.components([.Year, .Month], fromDate: sampleDate)
                        return components.month == sampleComponents.month && components.year == sampleComponents.year
                    })
                    numberOfDaysWithSamples[monthInedx] = Double(mothResults.count)
                    dispatch_group_leave(group)
                }
                dispatch_group_leave(group)
            }
        })
//        print("START QUERY EXEQUTE - \(NSDate())")
        healthKitStore.executeQuery(sampleQuery)
        dispatch_group_notify(group, dispatch_get_main_queue()) {
//            print("END OF CALCULATION - \(NSDate())")
            completion(numberOfDaysWithSamplesForEachMonth: numberOfDaysWithSamples)
        }
    }
    
    func getQueryForPeriod(statisticsRange: HealthManagerStatisticsRangeType, qType: HKQuantityType, statisticsOptions: HKStatisticsOptions) -> (HKStatisticsCollectionQuery, NSDate, NSDate) {
        let calendar = NSCalendar.currentCalendar()
        let interval = NSDateComponents()

                        if let maxValue = statistics.sumQuantity() {
                            doubleMaxValue = maxValue.doubleValueForUnit(quantityType.defaultUnit!)
                        }
                    }
                    statisticsValues.append(round(doubleMaxValue))//add max double value
                })
                completion(statisticsVlues: statisticsValues)
            }
        }
        healthKitStore.executeQuery(statisticsQuery)
    }
    
    /**
        This method will collect statistics for types Heart Rate and Blood Preasure
    */
    public func getMinMaxValuesForPeriod(statisticsRange: HealthManagerStatisticsRangeType, forType type: String, completion: (statisticsMaxVlues: [Double], statisticsMinVlues: [Double]) -> Void) {
        
        let group = dispatch_group_create()
