iphone,如果我可以'填充线程函数,我怎么能退出?

时间:2010-10-11 10:13:13

标签: iphone xcode

我已将我的数据库填充代码添加到线程方法中。但是,有时它们可​​能不是图表中显示的任何数据。我不想运行查询两次,之前检查是否有任何数据,我不想在线程函数之前预先填充图形点。

我在下面用HERE标记了我的填充代码。

我认为我唯一的选择是退出线程功能,但我有点担心,我想要正确地做到这一点,我需要做什么?

#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

我正在使用Tapku Graph http://duivesteyn.net/2010/03/07/iphone-sdk-implementing-the-tapku-graph-in-your-application/?utm_source=twitterfeed&utm_medium=twitter

1 个答案:

答案 0 :(得分:0)

要从线程方法中关闭线程,只需return。但是不要忘记致电[pool release];(请在方法结束时使用它而不是[pool drain];;在没有GC的iOS上,它们是相同的,但如果Apple有一天决定添加GC支持他们是不同的。)

所以它是这样的:

if (wantToCloseThread) {
    // Release everything we've allocated.
    [pool release];
    // Also, if you alloc'ed something that is not autoreleased
    // you should release it here.
    return;
}

另一种方法是使用goto(是的,它的使用可以):

- (void) thread {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    ...

    if (something)
       goto out;

    ...

    if (somethingElse)
       goto out;

    ...

out:
    // Cleanup.
    [pool release];
    // Also, if you alloc'ed something that is not autoreleased
    // you should release it here.
}

这样,你只需要编写一次清理,并且goto确保每次你想要实际离开线程时,完成清理工作。