Closure doesn't get all data

Discussion in 'iOS Programming' started by erdinc27, May 30, 2017.

  1. erdinc27 macrumors regular

    Joined:
    Jul 20, 2011
    #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?
     
  2. bjet767 macrumors 6502a

    Joined:
    Oct 2, 2010
    #2
    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
     

Share This Page