-1
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    //***********************************************************************************************************
    // requesting access and autherzation and availability
    let healthStore = HKHealthStore()
    if HKHealthStore.isHealthDataAvailable() {
        calciumTracker.setProgress(0.1, animated: true)
        let ShareTypes : Set = [HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!]
        let ReadTypes : Set = [HKCharacteristicType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!]
        healthStore.requestAuthorizationToShareTypes(ShareTypes, readTypes: ReadTypes , completion:{
            (success,error) in
            if !success {
                if let x = error {
                    print(x.description)
                }

            }
        })
    }
    else {
        print("Health data are not available on this device")
    }
    //***********************************************************************************************************
    let endDate = NSDate()
    let startDate = NSDate()
    var v : HKQuantitySample?
    let stepsCount:HKQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!
    let predicate:NSPredicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: .None)
    let limit = 1
    let query = HKSampleQuery(sampleType: stepsCount, predicate: predicate, limit: limit, sortDescriptors: nil, resultsHandler: {
        (query, results, error) in
            if results == nil {
                print(error)
            }
            v = results!.first as! HKQuantitySample?
        })
    healthStore.executeQuery(query)
    let l = Float(v!.quantity.doubleValueForUnit(HKUnit(fromString: "count")))
    stepsTracker.setProgress(l/300, animated: true)
}

when i run the app it get stuck on the launch screen and Xcode tell me there is threads an list them i do not understand any of them because i think they are in assembly language

  • You need to read up on how asynchronous methods work in iOS. You should put code that relies on the completion of any asynchronous methods inside the completion handlers. – dan Aug 07 '15 at 18:45

1 Answers1

0

The crash is caused by unwrapping value from nil object. The app tries to access HKQuantitySample even if it's nil, it crashes in this line:

let l = Float(v!.quantity.doubleValueForUnit(HKUnit(fromString: "count")))

You should re-organize the logic and execute the second half of the codes in the callback of the requestAuthorizationToShareTypes function.

For example:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    //***********************************************************************************************************
    // requesting access and autherzation and availability
    let healthStore = HKHealthStore()
    if HKHealthStore.isHealthDataAvailable() {
        calciumTracker.setProgress(0.1, animated: true)
        let ShareTypes : Set = [HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!]
        let ReadTypes : Set = [HKCharacteristicType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!]

        healthStore.requestAuthorizationToShareTypes(ShareTypes, readTypes: ReadTypes , completion:{
            (success,error) in
            if !success {
                if let x = error {
                    print(x.description)
                }
            } else {
                //***********************************************************************************************************
                let endDate = NSDate()
                let startDate = NSDate()
                var v : HKQuantitySample?
                let stepsCount:HKQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!
                let predicate:NSPredicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: .None)
                let limit = 1
                let query = HKSampleQuery(sampleType: stepsCount, predicate: predicate, limit: limit, sortDescriptors: nil, resultsHandler: {
                    (query, results, error) in
                    if results == nil {
                        print(error)
                    }
                    v = results!.first as! HKQuantitySample?
                })
                healthStore.executeQuery(query)
                let l = Float(v!.quantity.doubleValueForUnit(HKUnit(fromString: "count")))
                self.stepsTracker.setProgress(l/300, animated: true)
            }
        })
    }
    else {
        print("Health data are not available on this device")
    }
}

Ideally, before you request the authorization, you should check the authorization status first, i.e, call authorizationStatusForType().

utogaria
  • 3,662
  • 2
  • 14
  • 12