ios - multiple thread in assembly language -
override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. //*********************************************************************************************************** // requesting access , autherzation , 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 not available on 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 run app stuck on launch screen , xcode tell me there threads list them not understand of them because think in assembly language
the crash caused unwrapping value nil object. app tries access hkquantitysample if it's nil, crashes in line:
let l = float(v!.quantity.doublevalueforunit(hkunit(fromstring: "count")))
you should re-organize logic , execute second half of codes in callback of requestauthorizationtosharetypes function.
for example:
override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. //*********************************************************************************************************** // requesting access , autherzation , 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 not available on device") } }
ideally, before request authorization, should check authorization status first, i.e, call authorizationstatusfortype().
Comments
Post a Comment