ios - Healthkit: Data not showing first time running. -


i have strange problem when reading data out of healthkit. first time when press button weight printed 0, next times press button weight printed normal.i appreciate if me. thanks!

i have following code.

healtkitmanager.h

#import <foundation/foundation.h> #import <uikit/uikit.h> #import <healthkit/healthkit.h>  @interface healthkitmanager : nsobject @property (nonatomic) float weight; @property (nonatomic) hkhealthstore *healthstore;  +(healthkitmanager *)sharedmanager;  -(void) requestauthorization; -(double) readweight;     @end 

healthkitmanager.m

#import "healthkitmanager.h" #import <healthkit/healthkit.h> #import "startscreen.h" @interface healthkitmanager ()  @end    @implementation healthkitmanager  +(healthkitmanager *) sharedmanager{     static dispatch_once_t pred = 0;     static healthkitmanager *instance = nil;     dispatch_once(&pred, ^{         instance = [[healthkitmanager alloc]init];         instance.healthstore = [[hkhealthstore alloc]init];     });     return instance; }  -(void)requestauthorization {     if([hkhealthstore ishealthdataavailable]==no){         return;     }      nsarray *readtypes = @[[hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifierbodymass],                            [hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifierbodymassindex],                            [hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifierbodyfatpercentage]];      [self.healthstore requestauthorizationtosharetypes:nil readtypes:[nsset setwitharray:readtypes] completion:nil];  }  -(double) readweight{     nsmassformatter *massformatter = [[nsmassformatter alloc]init];     massformatter.unitstyle = nsformattingunitstylelong;      hkquantitytype *weighttype = [hkquantitytype quantitytypeforidentifier:hkquantitytypeidentifierbodymass];      [self fetchmostrecentdataofquantitytype:weighttype withcompletion:^(hkquantity *mostrecentquantity, nserror *error) {         if(!mostrecentquantity){             nslog(@"%@",@"error. ");         }         else{             hkunit *weightunit = [hkunit gramunit];             _weight = [mostrecentquantity doublevalueforunit:weightunit];             _weight = (float)_weight/1000.00f;         }     }];     return _weight; }  - (void)fetchmostrecentdataofquantitytype:(hkquantitytype *)quantitytype withcompletion:(void (^)(hkquantity *mostrecentquantity, nserror *error))completion {     nssortdescriptor *timesortdescriptor = [[nssortdescriptor alloc] initwithkey:hksamplesortidentifierenddate ascending:no];       hksamplequery *query = [[hksamplequery alloc] initwithsampletype:quantitytype predicate:nil limit:1 sortdescriptors:@[timesortdescriptor] resultshandler:^(hksamplequery *query, nsarray *results, nserror *error) {         if (!results) {             if (completion) {                 completion(nil, error);             }              return;         }          if (completion) {              hkquantitysample *quantitysample = results.firstobject;             hkquantity *quantity = quantitysample.quantity;              completion(quantity, error);         }     }];      [self.healthstore executequery:query]; }   @end 

startscreen.m

#import "startscreen.h" #import "healthkitmanager.h"  @interface startscreen () @property(nonatomic,weak) iboutlet uilabel *weightlabel;  @end  @implementation startscreen  - (void)viewdidload {     [super viewdidload];     [[healthkitmanager sharedmanager] requestauthorization]; }  - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }   -(ibaction)readagebuttonpressed:(id)sender{     healthkitmanager *readweight = [[healthkitmanager sharedmanager] init];    nslog(@"%.2f",readweight.readweight);  } @end 

you use singleton healthkitmanager code

healthkitmanager *readweight = [[healthkitmanager sharedmanager] init]; 

in ibaction readagebuttonpressed should be

healthkitmanager *readweight = [healthkitmanager sharedmanager]; 

you don't need init.

also, can change code

[[healthkitmanager sharedmanager] requestauthorization]; 

to be

healthkitmanager *readweight = [healthkitmanager sharedmanager]; [readweight requestauthorization]; nslog(@"%.2f",readweight.readweight); 

checking value of readweight.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -