Healthkit: Data not showing first time running.
<p>I have a strange problem when reading data out of healthkit. The first time when i press the button the weight is printed 0, the next times i press the button the weight is printed as normal.I would really appreciate it if someone could help me.
Thanks!   </p>

<p>I have the following code.</p>

<p><strong>HealtkitManager.h</strong></p>

<pre><code>#import &lt;Foundation/Foundation.h&gt;
#import &lt;UIKit/UIKit.h&gt;
#import &lt;HealthKit/HealthKit.h&gt;

@interface HealthKitManager : NSObject
@property (nonatomic) float weight;
@property (nonatomic) HKHealthStore *healthStore;

+(HealthKitManager *)sharedManager;

-(void) requestAuthorization;
-(double) readWeight;




@end
</code></pre>

<p><strong>Healthkitmanager.m</strong></p>

<pre><code>#import "HealthKitManager.h"
#import &lt;healthKit/healthKit.h&gt;
#import "StartScreen.h"
@interface HealthKitManager ()

@end



@implementation HealthKitManager

+(HealthKitManager *) sharedManager{
    static dispatch_once_t pred = 0;
    static HealthKitManager *instance = nil;
    dispatch_once(&amp;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
</code></pre>

<p><strong>Startscreen.m</strong></p>

<pre><code>#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 any resources that can be recreated.
}


-(IBAction)readAgeButtonPressed:(id)sender{
    HealthKitManager *readWeight = [[HealthKitManager sharedManager] init];
   NSLog(@"%.2f",readWeight.readWeight);

}
@end
</code></pre>