2015-05-26 48 views
0

我試圖從HealthKit中提取數據,正在以正確的速度提取數據。我認爲GCD建立的方式可能有問題。使用GCD加載數據非常慢

下面是代碼:

- (void)loadGraphDataForType:(NSString *)type withDuration:(int)graphDuration { 

    NSCalendar *calendar = [NSCalendar currentCalendar]; 
    NSDateComponents *interval = [NSDateComponents new]; 

    if (graphDuration == 1) { 

     interval.hour = 1; 

    } else { 

     interval.day = 1; 

    } 

    NSMutableArray *arrayOfValues = [NSMutableArray new]; 

    NSDate *anchorDate = [calendar startOfDayForDate:[NSDate date]]; 

    HKQuantityType *quantityType = [HKQuantityType quantityTypeForIdentifier:type]; 

    dispatch_queue_t queue = dispatch_queue_create([@"graph.queue" UTF8String], DISPATCH_QUEUE_CONCURRENT); 
    dispatch_group_t group = dispatch_group_create(); 

    dispatch_group_async(group, queue, ^{ 

     dispatch_semaphore_t lock = dispatch_semaphore_create(0); 

     HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType quantitySamplePredicate:nil options:HKStatisticsOptionCumulativeSum anchorDate:anchorDate intervalComponents:interval]; 

     query.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *results, NSError *error) { 

      if (error) { 

       NSLog(@"Error: %@ %@", error, [error userInfo]); 

      } else { 

       NSDate *endDate = [NSDate date]; 

       NSDate *startDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:-graphDuration toDate:endDate options:0]; 

       [results enumerateStatisticsFromDate:startDate toDate:endDate withBlock:^(HKStatistics *result, BOOL *stop) { 

        HKQuantity *quantity = result.sumQuantity; 

        if (quantity != nil) { 

         double value = 0; 

           //Steps Traveled 
           value = [quantity doubleValueForUnit:[HKUnit countUnit]]; 



         int intValue = (int)value; 

         [arrayOfValues addObject:@(intValue)]; 

        } else { 

         [arrayOfValues addObject:@0]; 

        } 

       }]; 

       dispatch_semaphore_signal(lock); 

      } 

     }; 

     [self.healthStore executeQuery:query]; 

     dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); 

    }); 

    dispatch_group_notify(group, queue, ^{ 

     graphData = [PNLineChartData new]; 
     graphData.color = [UIColor whiteColor]; 
     graphData.itemCount = [arrayOfValues count]; 
     graphData.getData = ^(NSUInteger index) { 

      CGFloat yValue = [arrayOfValues[index] floatValue]; 
      return [PNLineChartDataItem dataItemWithY:yValue]; 

     }; 

     graph.chartData = @[graphData]; 
     [graph strokeChart]; 

    }); 

} 

如果我把一個破發點,其中圖獲取項目計數,它顯示25(這是正確的),我可以看到陣列內的數據,但該圖需要5分鐘才能顯示出來。我現在嘗試了幾個不同的庫,它們都表現相同,所以我認爲它是我的代碼中的東西造成的。

我不是那麼熟悉GCD,所以我確信它是那樣的。

如果有人有任何建議,我將不勝感激。

謝謝!

回答

1

嘗試將此[graph strokeChart];包裝在dispatch_async中回到主隊列。

否則我會建議用儀器分析應用程序。

使用時間分析器檢查執行時間並查看大部分時間花費在哪裏。

然後,您可以檢查線程視圖以查看工作正在進行的位置以及發生何種等待的位置。

它看起來不像你需要派遣組來完成這項工作。

+1

你打敗了我,我在回答,我剛剛擺脫了所有的信號量和所有的垃圾。我只是將結果發送到每個Apple文檔的主隊列,現在一切正常。謝謝! https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HKStatisticsCollectionQuery_Class/ –

+0

很高興我能幫到你。 – jarryd