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

  • SEARCH
  • Home
  • 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 7735179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:26:11+00:00 2026-06-01T07:26:11+00:00

I have an iPhone application which in which I want to perform a method

  • 0

I have an iPhone application which in which I want to perform a method in the background every 1 second.

So in my main thread, I have the following code upon my UIViewController viewDidLoad():

[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(repeatedMethod) userInfo:nil repeats:YES];

with the following methods:

-(void)repeatedMethod {
  if(!processIsRunning) {
    processIsRunning = YES;
    [self performSelectorInBackground:@selector(runProcessInBackground:) withObject:myImage];
  }
}

-(void)runProcessInBackground:(UIImage *)image {
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  ... Some operations ...
  ...   also call a method on the main thread which sets isProcessRunning to NO

  [pool release]
}

The way I have this set up, a new thread is spawned every second (unless the process is still running, thanks to my processIsRunning flag).

Now, my questions are:

(1) Is this the best way to do this? Or is there a more appropriate way to retain and reuse the background thread?

(2) What might be the fastest way to do this? Am I losing time by spinning up new background threads each time the method is called?

The code works fine as is, it’s just a quite a bit slower when I ran everything on the main thread (which I ultimately don’t want to do).

Any advice would be great!
Has anyone dealt with this type of question before?

Many thanks,
Brett

  • 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-06-01T07:26:12+00:00Added an answer on June 1, 2026 at 7:26 am

    Based on the question in your comment, I figured I’d expand my comment into a real answer. You can use GCD for this, and it’s likely best way to go.

    Something like this:

    dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, backgroundQueue);
    dispatch_source_set_timer(timerSource, dispatch_time(DISPATCH_TIME_NOW, 0), 1.0*NSEC_PER_SEC, 0*NSEC_PER_SEC);
    dispatch_source_set_event_handler(timerSource, ^{
        [self repeatedMethod];
    });
    dispatch_resume(timerSource);
    
    - (void)repeatedMethod {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        [self doStuffWithAnImage:self.myImage];   
    
        [pool drain];
    }
    

    One of the coolest things about blocks is that they capture local scope. That’s not actually used in my example above, but it makes “passing” objects into GCD trivial. See my answer to this question: Is there an advantage to using blocks over functions in Objective-C? .

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

Sidebar

Related Questions

Background: I have an iPhone application for which I want the launch image to
I have an existing iPhone application which starts from a UIViewController. What I want
I have an application in IPhone which is now on sale.I want to submit
I have an iPhone application which has a storyboard. Now I want to provide
I have an application in which I want to access the iPod and iPhone
I have an iphone application which is having a UIWindow. I want to add
I have created an iPhone application which I want to deploy onto an iPhone.
I have an iphone application which has about 4 tabs. How do I force
I have an iPhone application which basically is getting information from an API (in
I have an IPhone application which is having a non consumable in app purchse

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.