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 
}