First, what's the output of this?
NSLog(@"steps array = %@", _stepsArray);
When someone ask you something, you should try to reply with the exact information requested, just saying "it says that array is empty" doesn't help, because maybe someone can see something that you don't see in the output.
Said this, I would add some more NSLog around, because it could be that your handler is not called, or not called with the information that you expect.
Use the following, and let us know the output :)
[self.cmStepCounter queryStepCountStartingFrom:fromDate to:toDate toQueue:self.operationQueue withHandler:^(NSInteger numberOfSteps, NSError *error) {
if (!error) {
NSLog(@"queryStepCount returned %d steps", numberOfSteps);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self updateArrayWithStepCounter:numberOfSteps];
}];
} else {
NSLog(@"Error occured: %@", error.localizedDescription);
}
}];
EDIT: From the new posted output of the NSLog, I can understand the problem. The fact is that the handler runs asynchronously, it means that you can't just output the array on viewDidLoad, because it runs BEFORE the array received all the values, so you should refactor your code to trigger a method when all the data is ready.
Here a revision of your code that is more readable (removed some useless "copy" call, updated your "for conditions", etc...), now it should be really easy to understand what's going on, and how to perform additional logic.
#import "PYViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface PYViewController ()
@property (weak, nonatomic) IBOutlet UILabel *stepsCountingLabel;
@property (nonatomic, strong) CMStepCounter *cmStepCounter;
@property (nonatomic, strong) NSOperationQueue *operationQueue;
@property (nonatomic, strong) NSMutableArray *stepsArray;
@end
@implementation PYViewController
- (NSOperationQueue *)operationQueue {
if (_operationQueue == nil) {
_operationQueue = [NSOperationQueue new];
_operationQueue.maxConcurrentOperationCount = 1; // process 1 operation at a time, or we could end with unexpected results on _stepsArray
}
return _operationQueue;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self queryExistingStep];
}
-(void)queryExistingStep {
// Get now date
NSDate *now = [NSDate date];
// Array to hold step values
_stepsArray = [[NSMutableArray alloc] initWithCapacity:7];
// Check if step counting is avaliable
if ([CMStepCounter isStepCountingAvailable]) {
// Init step counter
self.cmStepCounter = [[CMStepCounter alloc] init];
// Tweak this value as you need (you can also parametrize it)
NSInteger daysBack = 6;
for (NSInteger day = daysBack; day > 0; day--) {
NSDate *fromDate = [now dateByAddingTimeInterval: -day * 24 * 60 * 60];
NSDate *toDate = [fromDate dateByAddingTimeInterval:24 * 60 * 60];
[self.cmStepCounter queryStepCountStartingFrom:fromDate to:toDate toQueue:self.operationQueue withHandler:^(NSInteger numberOfSteps, NSError *error) {
if (!error) {
NSLog(@"queryStepCount returned %ld steps", (long)numberOfSteps);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[_stepsArray addObject:@(numberOfSteps)];
if ( day == 1) { // Just reached the last element, do what you want with the data
NSLog(@"_stepsArray filled with data: %@", _stepsArray);
// [self updateMyUI];
}
}];
} else {
NSLog(@"Error occured: %@", error.localizedDescription);
}
}];
}
} else {
NSLog(@"device not supported");
}
}
@end