Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3224830
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:16:08+00:00 2026-05-17T16:16:08+00:00

I’ve added my database populate code to the thread method. However, sometimes they may

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:16:09+00:00Added an answer on May 17, 2026 at 4:16 pm

    To close the thread, just return from the thread method. But don’t forget to call [pool release]; (please use it instead of [pool drain]; at the end of your method as well; on iOS where there’s no GC they are the same but if Apple one day decides to add GC support they’re different).

    So it’s something like this:

    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;
    }
    

    An alternative is to use goto (yes, its use for this is OK):

    - (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.
    }
    

    This way, you have to write the cleanup only once and the goto ensures that every time you want to actually leave the thread, the complete cleanup is done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.