0
#import "CPPedometerViewController.h" 
#import <CoreMotion/CoreMotion.h> 

@interface CPPedometerViewController() 

@property (weak, nonatomic) IBOutlet UILabel *stepsCountingLabel; 
@property (nonatomic, strong) CMStepCounter *cmStepCounter; 
@property (nonatomic, strong) NSOperationQueue *operationQueue; 
@property (nonatomic, strong) NSMutableArray *stepsArray; 

@end 

@implementation CPPedometerViewController 

- (NSOperationQueue *)operationQueue 
{ 
    if (_operationQueue == nil) 
    { 
     _operationQueue = [NSOperationQueue new]; 
    } 
    return _operationQueue; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self QueryExistingStep]; 


    NSLog(@"steps array = %@", _stepsArray); 

} 

-(void)QueryExistingStep 
{ 
    //get todays date 
    NSDate *now = [NSDate date]; 
    // get six days ago from today 
    NSDate *sixDaysAgo = [now dateByAddingTimeInterval:-6*24*60*60]; 

    //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]; 

     //get seven days before from date & to date. 
     for (NSDate *toDate = [sixDaysAgo copy]; [toDate compare: now] <= 0; 
      toDate = [toDate dateByAddingTimeInterval:24 * 60 * 60]) { 

      //get day before 
      NSDate *fromDate = [[toDate copy] dateByAddingTimeInterval: -1 * 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:^{ 
         [self updateArrayWithStepCounter:numberOfSteps]; 
        }]; 
       } else { 
        NSLog(@"Error occured: %@", error.localizedDescription); 
       } 

      }]; 

     } 
    } else { 
     // stuffhappens 
    } 
} 

- (void)updateArrayWithStepCounter:(NSInteger)numberOfSteps { 

    [_stepsArray addObject:[NSNumber numberWithInteger:numberOfSteps]]; 
} 

@end 

我期待有一个数组完整的过去七天的步骤,然后将它们插入到NSinteger为每一天。例如NSinteger daySeven = 242,NSInteger daySix = 823 ...等等今天。CMStepCounter添加步骤来分离NSIntegers

但是,退出updateArrayWithStepCounter方法后数组似乎已清除。任何关于如何解决这个问题的想法,以便每个步骤都进入单独的NSIntegers。谢谢,瑞安。

编辑:

这里是输出的NSLog:

2014-01-25 22:51:36.314 Project[6633:60b] steps array = (
) 
2014-01-25 22:51:36.332 Project[6633:420f] queryStepCount returned 3505 steps 
2014-01-25 22:51:36.334 Project[6633:420f] queryStepCount returned 3365 steps 
2014-01-25 22:51:36.335 Project[6633:420f] queryStepCount returned 7206 steps 
2014-01-25 22:51:36.337 Project[6633:420f] queryStepCount returned 6045 steps 
2014-01-25 22:51:36.339 Project[6633:420f] queryStepCount returned 5259 steps 
2014-01-25 22:51:36.342 Project[6633:420f] queryStepCount returned 6723 steps 
2014-01-25 22:51:36.344 Project[6633:420f] queryStepCount returned 440 steps 

这里是所建议所示的输出。正如你所看到的,当它在运行该方法后检查数组时,它肯定会获取值,但现在它是空的。我可以不正确地将它添加到数组吗?

我希望这更清楚我很难过。谢谢

回答

2

首先,这是什么输出?

NSLog(@"steps array = %@", _stepsArray); 

当有人问你,你应该尝试与要求的确切信息回复,只是说“它说,数组为空”没有帮助,因为也许有人可以看到的东西,你不在输出中看到。

说这个,我会添加一些更多的NSLog,因为它可能是你的处理程序没有被调用,或者没有用你期望的信息调用。

使用下面,让我们知道输出:)

编辑:从NSLog的新发布的输出,我能理解这个问题。事实是,处理程序异步运行,这意味着你不能只在viewDidLoad上输出数组,因为它在数组收到所有值之前运行,所以你应该重构代码以在所有数据准备好时触发一个方法。

在这里,你的代码更具可读性(删除了一些无用的“复制”调用,更新了你的“条件”等)的修订版,现在应该很容易理解发生了什么,以及执行额外的逻辑。

#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 
+0

通过它回来到viewDidLoad方法时_stepsArray打印出空:( – Ryan

+0

这不的问题提供一个答案,以批判或作者作出澄清,离开低于其信息的评论中 - 你可以随时评论你自己的帖子,一旦你有足够的[声誉](http://stackoverflow.com/help/whats-reputation)你将能够[评论任何帖子](http://stackoverflow.com/help/privileges/comment) –

+0

我最初发送了一个问题,因为我没有足够的声望发布对他的问题的评论,但我只是扩展了我的答案:-) – Daniele

0

我会做的是等待阵列来获得所有需要的价值,后来做的东西。这个对我有用。

- (void)queryPast6DayStepCounts 
{ 
    NSLog(@"queryPast6DayStepCounts visited!"); 

    self.past6DaysDailySteps = [[NSMutableArray alloc] initWithCapacity:6]; 

// past 6 days 

for (NSInteger day = 6; day >= 1; day--) 
{ 
    NSDate *fromDate = [self.todayMidnight dateByAddingTimeInterval:-day * 24 * 60 * 60]; 
    NSDate *toDate = [fromDate dateByAddingTimeInterval:24 * 60 * 60]; 

    [self.cmStepCounter 
    queryStepCountStartingFrom:fromDate to:toDate 
    toQueue:[NSOperationQueue mainQueue] 
    withHandler:^(NSInteger numberOfSteps, NSError *error) 
    { 
     if (!error) 
     { 
       [self.past6DaysDailySteps addObject:@(numberOfSteps)]; 
       if (self.past6DaysDailySteps.count == 6) { 
        [self viewDidFinishQueryPast6DaysData]; 
       } 

     } 
     else 
     { 
      NSLog(@"Error occured: %@", error.localizedDescription); 
     } 
    } 
    ]; 

} 
} 
-(void)viewDidFinishQueryPast6DaysData 
    { 
    NSLog(@"queryPast6DayStepCounts finish! %@", self.past6DaysDailySteps); 
    // do other things 
}