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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:38:54+00:00 2026-06-04T00:38:54+00:00

If I have an innocuous loop that gets fired off when I press a

  • 0

If I have an innocuous loop that gets fired off when I press a button like:

- (IBAction)start:(id)sender{
    calculatingProgressBar.progress = 0.0;  
    int j = 1000;
    for (int i=0; i<j; i++) {
        calculatingProgressBar.progress = (i/j);
    }
}

The ProgressView is not updated. In other languages there is a view update command that you have to execute in these circumstances. What is the Cocoa equivalent or do you have to effect some other workaround.

  • 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-04T00:38:56+00:00Added an answer on June 4, 2026 at 12:38 am

    The problem is that the UI is only updated periodically as the run (event) loop turns. A for() loop, which I am assuming runs on the main thread, is going to block the run loop until finished. Not only does this mean the progress bar won’t be updated (or, rather, will jump right to 100% at the end), but that you are potentially going to freeze the UI for some time. (I assume, since you are showing a progress bar, that this could take a while.)

    What you want to do, for any lengthy operation, is to have the work done on a separate queue/thread, and call out the update the UI on the main thread periodically. For example, you might try something like (warning: typed in browser):

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
    dispatch_async(queue, ^{
        int j = 1000;
        for (int i=0; i<j; i++) {
            // Do your presumably useful work, not just looping for fun :-)
            dispatch_async(dispatch_get_main_queue(), ^{
                // UI updates always come from the main queue!
                calculatingProgressBar.progress = (i/j);
            });
        }
    });
    

    This example uses GCD (Grand Central Dispatch). There are higher level constructs you can also look at, like NSOperationQueue, but the basic idea remains the same.

    If you are unfamiliar with concurrent (multithreaded) programming, you are going to have a modest learning curve to get up to speed. I suggest starting with the Concurrency Programming Guide.

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

Sidebar

Related Questions

have a small javscript that users can include into their sites like so: <script
Have a digital media device that I'd like to get the ImageMagick command-line tool
Have a simple one-off tasks which needs a progress bar. OpenSSL has a useful
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
Have a matrix report now that has Position, Hours and Wages for a location
Have searched the database but need to specifically sum(of hours flown or days off)in
Have a bunch of WCF REST services hosted on Azure that access a SQL
Have a look at one of my websites: moskah.com The problem is that it
We have a Wicket app with a page that includes an embedded Youtube video.
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a

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.