Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
In an application I use HealthKit. I get the walking and running information. I can display the data in log screen but when I want to send the data to view controller with a closure it sends only 10 or 15 rows but not the all. Here how my code looks like.
Code:
- (void) readStepCounterWithCompletionHandler:(void (^)(BOOL isErrorOccured, NSString *errorDesc, NSArray *sampleArray)) handler {
   
    NSDate *startDate;
    NSDate *endDate = [NSDate date];
   
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
  
    // Use the sample type for step count
    HKSampleType *sampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];
   
    // Create a predicate to set start/end date bounds of the query
    NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate];
   
    // Create a sort descriptor for sorting by start date
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:false];
   
    HKSampleQuery *query2 = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:predicate limit:HKObjectQueryNoLimit sortDescriptors:@[sortDescriptor] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {
       
        if(error) {
       
            handler(true, error.description, nil);
        } else {
       
                            NSMutableArray *tempArray = [NSMutableArray new];
           
                            for(HKQuantitySample *samples in results)  {
                                [tempArray addObject:samples];
                                NSLog(@"Samples %@", samples);
                            }
           
            handler(false, @"", tempArray);
        }
    }];
   
    // Execute the query
    [self.healthStore executeQuery:query2];
}

How can I send the all data to view controller?
 
The reason it doesn't show everything is because of the timing of the events.

In you handler use something like this:


Code:
//Do something
dispatch_sync(queue, ^{
  //Do something else
});
//Do More Stuff
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.