I've added my database populate code to the thread method. However, sometimes they may not be any data to show in the graph. I don't want to run the query twice, once before to check if theres any data and I don't want to pre-populate the graph points prior to the thread function.
I've marked where I have my populate code with HERE below.
I think my only option is to exit the thread function, but I'm a little concerned and I want to do this correctly, what do i need to do ?
#import "GraphController.h"
@implementation GraphPoint
- (id) initWithID:(int)pkv value:(NSNumber*)number{
if(self = [super init]){
pk = pkv;
value = [number retain];
}
return self;
}
- (NSNumber*) yValue{
return value;
}
- (NSString*) xLabel{
return [NSString stringWithFormat:@"%d",pk];
}
- (NSString*) yLabel{
return [NSString stringWithFormat:@"%d",[value intValue]];
}
@end
@implementation GraphController
- (void)viewDidLoad{
[super viewDidLoad];
graph.title.text = @"Graph View";
[graph setPointDistance:15];
indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
CGRect r = indicator.frame;
r.origin = self.view.bounds.origin;
r.origin.x = self.view.bounds.size.width / 2 - r.size.width / 2;
r.origin.y = self.view.bounds.size.height / 2 - r.size.height / 2;
indicator.frame = r;
[self.view addSubview:indicator];
[indicator startAnimating];
data = [[NSMutableArray alloc] init];
[NSThread detachNewThreadSelector:@selector(thread) toTarget:self withObject:nil];
}
- (void) thread{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//HERE
srand([[NSDate date] timeIntervalSince1970]);
for(int i=0;i<100;i++){
int no = rand() % 100 + i;
GraphPoint *gp = [[GraphPoint alloc] initWithID:i value:[NSNumber numberWithFloat:no]];
[data addObject:gp];
[gp release];
}
[self performSelectorOnMainThread:@selector(threadComplete) withObject:nil waitUntilDone:NO];
[pool drain];
}
- (void) threadComplete{
[indicator stopAnimating];
[self.graph setGraphWithDataPoints:data];
self.graph.goalValue = [NSNumber numberWithFloat:30.0];
self.graph.goalShown = YES;
[self.graph scrollToPoint:80 animated:YES];
[self.graph showIndicatorForPoint:75];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[data release];
[indicator release];
[super dealloc];
}
@end
I'm using Tapku Graph http://duivesteyn.net/2010/03/07/iphone-sdk-implementing-the-tapku-graph-in-your-application/?utm_source=twitterfeed&utm_medium=twitter