1

I need to get the final activeTime value once the loop has completed.

The code below runs a loop, which in turn calls a block many times (which runs an async process). Each loop will increment activeTime if more than 10 steps occurred in that period.

Ultimately I want to call this code and have it return the final value of activeTime.

I am thinking of completionhandler but am not sure.

Can someone advise how I may achieve this?

    __block NSInteger activeTime = 0;
    for (NSDate *date = [self startDateOfToday]; 
         [date compare:[NSDate date]] == NSOrderedAscending; 
         date = [date dateByAddingTimeInterval:10])        
    {
        NSDate *rangeStart = date;
        NSDate *rangeEnd = [date dateByAddingTimeInterval:10];

        [stepCounter queryStepCountStartingFrom:rangeStart
                                             to:rangeEnd
                                        toQueue:[NSOperationQueue mainQueue]
                                    withHandler:^(NSInteger numberOfSteps, NSError *error) {

                                        if (numberOfSteps > 10) {
                                            activeTime=activeTime+10;
                                        }

                                        NSLog(@"Steps = %ld; Time = %ld",(long)numberOfSteps, (long)activeTime);
                                    }];
    }
NeilMortonNet
  • 1,500
  • 4
  • 16
  • 36
  • A thought I have had is is there anyway to call the for loop to itself return the final value and do something with it on completion? I may be way off the mark, but it was just another idea that crossed my mind.? – NeilMortonNet Feb 27 '14 at 10:10
  • how about making _activeTime_ static like `static NSInteger activeTime = 0;` – Sathya Feb 27 '14 at 12:54
  • Where do you mean to do this? Also would that make the code wait for the result? As at the moment there I have no way of knowing when the for loop has completed and thus no way of using the value? – NeilMortonNet Feb 27 '14 at 13:04
  • if you don't know when the loop will be completed, you could use a callback. may be in `if(rangeEnd == numberOfSteps) { [self callback: activeTime] }` – Sathya Feb 27 '14 at 13:10
  • Thank you. I shall have a look at that. Sounds promising. Can I ask, Where would you place that if statement in the code above? – NeilMortonNet Feb 27 '14 at 13:21
  • you could add it above your 'nslog' – Sathya Feb 27 '14 at 13:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48583/discussion-between-sathya-and-mrneilm) – Sathya Feb 27 '14 at 13:24

1 Answers1

1

You could use a callback

- (void) myfunction {
    __block NSInteger activeTime = 0;
    for (NSDate *date = [self startDateOfToday]; 
         [date compare:[NSDate date]] == NSOrderedAscending; 
         date = [date dateByAddingTimeInterval:10])        
    {
        NSDate *rangeStart = date;
        NSDate *rangeEnd = [date dateByAddingTimeInterval:10];

        [stepCounter queryStepCountStartingFrom:rangeStart
                                             to:rangeEnd
                                        toQueue:[NSOperationQueue mainQueue]
                                    withHandler:^(NSInteger numberOfSteps, NSError *error) {

                                        if (numberOfSteps > 10) {
                                            activeTime=activeTime+10;
                                        }

                                        if(rangeEnd == numberOfSteps)
                                        {
                                            [self callback:activeTime];
                                        }

                                        NSLog(@"Steps = %ld; Time = %ld",(long)numberOfSteps, (long)activeTime);
                                    }];
    }
}

- (void)callback:(NSInteger)activeTime {

    NSLog(@"Active Time = %d", activeTime);
}
Sathya
  • 2,197
  • 4
  • 22
  • 25
  • That was great thanks Sathya. I just had to swap the rangeend == numberOfSteps to a date compare to check for the loop completing. – NeilMortonNet Feb 27 '14 at 17:38