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 6579797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:56:35+00:00 2026-05-25T15:56:35+00:00

I’m an experienced C/C++ programmer coming up to speed on Objective C on the

  • 0

I’m an experienced C/C++ programmer coming up to speed on Objective C on the iPhone. I have done a lot of searching, but haven’t found a satisfactory answer on what must be a common question; I apologize if this is answered elsewhere, pointers would be appreciated.

My app is very CPU intensive. The UI has a simple display that shows progress and a start/stop button. What is the best way to allocate the most possible CPU cycles to getting the work done, while still ensuring that the display is updated regularly and the start/stop button is responsive? I have read that you should not do work in the main thread, but beyond that I haven’t found many suggestions. In light of this I have implemented my work in an NSOperation queue. I have also put the screen refresh in its own queue. I have also liberally sprinkled the code with NSThread sleepForTimeIntervals. I have experimented with different sleep times from .001 to 1 ([NSThread sleepForTimeIntervals .1] for instance). In spite of this the screen display is sluggish at best (10s of seconds) and pressing the stop button highlights the button but nothing happens again for 10s of seconds.

1.) Are NSOperation Queues a reasonable choice? If not, what else?
2.) How do I minimize the sleeping? (Obviously I want the work to get as many cycles as possible/reasonable, and I’m not sure that my sleeps are doing anything at all to all the UI to update.)
3.) Is there a better technique to keep the UI up to date? For instance, can I use NSTimer or some other method to send a message to the UI telling it to update and/or check the status of the buttons?

Thank you for your support.

  • 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-25T15:56:35+00:00Added an answer on May 25, 2026 at 3:56 pm

    1.) Are NSOperation Queues a reasonable choice? If not, what else?

    NSOperationQueue sounds like it would be reasonable.

    of course, you have choice: pthreads, libdispatch (aka GCD), c++ thread libraries built on top of pthreads, etc, etc , etc. if you don’t spawn much/many, then it just comes down to the model you favor.

    2.) How do I minimize the sleeping? (Obviously I want the work to get as many cycles as possible/reasonable, and I’m not sure that my sleeps are doing anything at all to all the UI to update.)

    don’t sleep =) you can use a timer for your ui elements or an explicit callback or notification to notify dependencies. if the dependencies peform ui updates, then you will likely add the message to the main thread’s message queue.

    3.) Is there a better technique to keep the UI up to date? For instance, can I use NSTimer or some other method to send a message to the UI telling it to update and/or check the status of the buttons?

    that really depends on what you are doing. if you merely want to update a progress bar, then you can write the value from the secondary thread and read the value from the main thread. then use a timer on the main run loop to periodically message your object to update its display (based on the current value). for something like an unstaged progress indicator this may be good.

    another alternative is more useful for events or stages: it would involve posting updates (e.g. notifications or callbacks to a delegate) from the secondary thread as progress is made (more info under #2).

    Update

    I wasn’t sure this was appropriate in the iOS model, but it sounds like it is.

    yes, that’s fine – there are many appraches you can take. which is ‘best’ depends on the context.

    My current understanding is to launch the UI in one thread (not the main!),

    you really don’t explicitly launch the UI; the main thread is (generally) driven by pushing events and messages onto the main thread. the main thread uses a run loop and processes the queued messages/events at each iteration of the run loop. you can also schedule these messages in the future (more on that in a bit). having said that, all your messages to UIKit and AppKit (if you target osx) objects should be on the main thread (as a generalization which you will eventually learn there are exceptions to this). if you have a specific implementation which is completely separated from messaging UIKit objects’ methods and that program is thread safe, then you can actually perform those messages from any thread because it does not affect the state of the UIKit implementation. simplest example:

    @interface MONView : UIView
    @end
    
    @implementation MONView
    // ...
    - (NSString *)iconImageName { return @"tortoise.png"; } // pure and threadsafe
    @end
    

    launch my worker thread, use a timer to generate a signal to the UI to take a look at a progress value and update the progress bar appropriately. For the purposes of this particular application your second to last paragraph is ample and I don’t need to go to the lengths of the last paragraph (at least for now). Thank you.

    to do this, you can use an approach similar to this:

    @interface MONView : UIView
    {
        NSTimer * timer;
        MONAsyncWorker * worker; // << this would be your NSOperation subclass, if you use NSOperation.
    }
    
    @end
    
    @implementation MONView
    
    // callback for the operation 'worker' when it completes or is cancelled.
    - (void)workerWillExit
    {
        assert([NSThread isMainThread]); // call on main
    
        // end recurring updates
        [self.timer invalidate];
        self.timer = nil;
    
        // grab what we need from the worker
        self.worker = nil;
        // update ui
    }
    
    // timer callback
    - (void)timerUpdateCallback
    {
        assert([NSThread isMainThread]); // call on main
        assert(self.worker);
    
        double progress = self.worker.progress;
    
        [self updateProgressBar:progress];
    }
    
    // controller entry to initiate an operation
    - (void)beginDownload:(NSURL *)url
    {
        assert([NSThread isMainThread]); // call on main
        assert(nil == worker); // call only once in view's lifetime
    
        // create worker
        worker = [[MONAsyncWorker alloc] initWithURL:url];
        [self.operationQueue addOperation:worker];
    
        // configure timer
        const NSTimeInterval displayUpdateFrequencyInSeconds = 0.200;
        timer = [[NSTimer scheduledTimerWithTimeInterval:displayUpdateFrequencyInSeconds target:self selector:@selector(timerUpdateCallback) userInfo:nil repeats:YES] retain];
    }
    
    @end
    

    note that this is a very primitive demonstration. it’s also more common to put the timer, update handling, and operation in the view’s controller, not the view.

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

Sidebar

Related Questions

I am an experienced programmer and have used OOP a lot, so I'm very
Most of experienced programmer knows data alignment is important for program's performance. I have
I am relatively new to PHP, but experienced Java programmer in complex enterprise environments
I'm an experienced programmer, but relatively new to SQL. We're using Oracle 10 and
Im a pretty experienced programmer in PHP and mainly web languages but today i
This question is not coming from a programmer. (obviously) I currently have a programmer
I am an experienced programmer in C, yet not objective c. I am trying
I am (quite) an experienced programmer but totally new to Ruby and Ruby on
I'm an experienced programmer (PHP/mySQL, jQuery), but know nothing about ColdFusion. A precursory Google
I'm an experienced programmer, but a novice to XSLT and am finding it quite

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.