我刚刚开始使用 Core Plot,为了测试,CPTGraphHostingView
嵌入了一个简单的自定义视图控制器,绘制来自 Core Data fetchRequest 的值(这是一个绘制每天膳食卡路里摄入量的应用程序)。
代码大部分是从这里的教程中粘贴的。
问题是在将视图控制器推入视图时 UI 冻结了大约两秒钟(它嵌入在导航控制器中)。这是在设备 (iPhone 4S) 上运行时。
Instruments 中的分析显示主线程被[CPTAxis layoutSublayers]
and阻塞[CPTLayer drawInContext]
。
滞后不是由于数据集过大:它目前正好包含两个点。该图非常简单,如下所示:
视图控制器的完整实现:
界面:
//
// ICCaloriesGraphController.h
// iCalories
//
// Created by David Fearon on 22/08/2013.
// Copyright (c) 2013 David Fearon. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
#import "ICDatabaseBrain.h"
#import "Day.h"
#import "CalorieEntry.h"
@interface ICCaloriesGraphController : UIViewController<CPTPlotDataSource, NSFetchedResultsControllerDelegate>
@property (weak, nonatomic) IBOutlet CPTGraphHostingView *graphHostingView;
@property (nonatomic, retain)ICDatabaseBrain* sharedDatabaseBrain;
@property (nonatomic, retain)NSFetchedResultsController* resultsController;
@end
执行:
//
// ICCaloriesGraphController.m
// iCalories
//
// Created by David Fearon on 22/08/2013.
// Copyright (c) 2013 David Fearon. All rights reserved.
//
#import "ICCaloriesGraphController.h"
@interface ICCaloriesGraphController ()
@property int numberOfRecordsForPlot;
@property float maxDailyCaloriesInDataset;
@property NSArray* days;
@end
@implementation ICCaloriesGraphController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.sharedDatabaseBrain = [ICDatabaseBrain sharedDatabaseBrain];
self.resultsController = self.sharedDatabaseBrain.fetchDaysResultsController;
self.days = [self.resultsController fetchedObjects];
self.numberOfRecordsForPlot = [self.days count];
[self updateMaxDailyCaloriesInDataset];
// Create a CPTGraph object and add to hostView
CPTGraph* graph = [[CPTXYGraph alloc] initWithFrame:self.graphHostingView.bounds];
self.graphHostingView.hostedGraph = graph;
// Get the (default) plotspace from the graph so we can set its x/y ranges
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) self.graphHostingView.hostedGraph.defaultPlotSpace;
// Note that these CPTPlotRange are defined by START and LENGTH (not START and END) !!
[plotSpace setYRange: [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat( self.maxDailyCaloriesInDataset )]];
[plotSpace setXRange: [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat( self.numberOfRecordsForPlot )]];
// Create the plot (we do not define actual x/y values yet, these will be supplied by the datasource...)
CPTScatterPlot* plot = [[CPTScatterPlot alloc] initWithFrame:CGRectZero];
// Let's keep it simple and let this class act as datasource (therefore we implement <CPTPlotDataSource>)
plot.dataSource = self;
// Finally, add the created plot to the default plot space of the CPTGraph object we created before
[self.graphHostingView.hostedGraph addPlot:plot toPlotSpace:self.graphHostingView.hostedGraph.defaultPlotSpace];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plotnumberOfRecords {
return self.numberOfRecordsForPlot;
}
-(void)updateMaxDailyCaloriesInDataset{
if (self.numberOfRecordsForPlot == 0){
self.maxDailyCaloriesInDataset = 0;
return;
}
int max = 0;
for (Day* day in self.days) {
//count the calories in the day:
int calorieSum = 0;
for (CalorieEntry* calorieEntry in [day.calorieEntries allObjects]) {
calorieSum += calorieEntry.calories.intValue;
}
if(calorieSum > max) max = calorieSum;
}
self.maxDailyCaloriesInDataset = max;
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
if (self.numberOfRecordsForPlot == 0){
return 0;
}
if(fieldEnum == CPTScatterPlotFieldY){
Day* day = [self.days objectAtIndex:index];
//count the calories in the day:
int calorieSum = 0;
for (CalorieEntry* calorieEntry in [day.calorieEntries allObjects]) {
calorieSum += calorieEntry.calories.intValue;
}
return [NSNumber numberWithInt:calorieSum];
//TODO: Set range for y axis
}
if(fieldEnum == CPTScatterPlotFieldX){
return [NSNumber numberWithInt: index];
}else{
NSLog(@"fieldEnum was neither CPTScatterPlotFieldY or CPTScatterPlotFieldX in numberForPlot:");
return 0;
}
}
@end
延迟绝对与 Core Data 代码无关;一切正常。谷歌搜索只会显示大型数据集的问题,而不是只有两点的问题。显然,我对核心情节代码做了一些根本性的错误,但真的看不出是什么。
请问有人能发现问题吗?