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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:31:25+00:00 2026-05-25T09:31:25+00:00

I’m having a hard time figuring out how to put this all together. I

  • 0

I’m having a hard time figuring out how to put this all together.
I have a puzzle solving app on the mac.
You enter the puzzle, press a button, and while it’s trying to find the number of solutions,
min moves and such I would like to keep the UI updated.
Then once it’s finished calculating, re-enable the button and change the title.

Below is some sample code from the button selector, and the solving function:
( Please keep in mind I copy/paste from Xcode so there might be some missing {} or
some other typos.. but it should give you an idea what I’m trying to do.

Basicly, user presses a button, that button is ENABLED=NO, Function called to calculate puzzle. While it’s calculating, keep the UI Labels updated with moves/solution data.
Then once it’s finished calculating the puzzle, Button is ENABLED=YES;

Called when button is pressed:

- (void) solvePuzzle:(id)sender{
    solveButton.enabled = NO;
    solveButton.title = @"Working . . . .";

    // I've tried using this as a Background thread, but I can't get the code to waitTilDone before continuing and changing the button state.
    [self performSelectorInBackground:@selector(createTreeFromNode:) withObject:rootNode];

    // I've tried to use GCD but similar issue and can't get UI updated.
    //dispatch_queue_t queue = dispatch_queue_create("com.gamesbychris.createTree", 0);
    //dispatch_sync(queue, ^{[self createTreeFromNode:rootNode];});

    }

    // Need to wait here until createTreeFromNode is finished.
    solveButton.enabled=YES;
    if (numSolutions == 0) {
    solveButton.title = @"Not Solvable";
    } else {
        solveButton.title = @"Solve Puzzle";
    }
}

Needs to run in background so UI can be updated:

-(void)createTreeFromNode:(TreeNode *)node
{
   // Tried using GCD
   dispatch_queue_t main_queue = dispatch_get_main_queue();

 ...Create Tree Node and find Children Code...

if (!solutionFound){
    // Solution not found yet so check other children by recursion.
   [self createTreeFromNode:newChild];
   } else {
   // Solution found.
   numSolutions ++;
   if (maxMoves < newChild.numberOfMoves) {
       maxMoves = newChild.numberOfMoves;
    }
    if (minMoves < 1 || minMoves > newChild.numberOfMoves) {
        solutionNode = newChild;
        minMoves = newChild.numberOfMoves;

        // Update UI on main Thread

        dispatch_async(main_queue, ^{
                        minMovesLabel.stringValue = [NSString stringWithFormat:@"%d",minMoves];
                        numSolutionsLabel.stringValue = [NSString stringWithFormat:@"%d",numSolutions];
                        maxMovesLabel.stringValue = [NSString stringWithFormat:@"%d",maxMoves];
                    });
                }                        
  • 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-25T09:31:25+00:00Added an answer on May 25, 2026 at 9:31 am

    GCD and performSelectorInBackground samples below. But first, let’s look at your code.

    You cannot wait where you want to in the code above.
    Here’s the code you had. Where you say wait in the comment is incorrect. See where I added NO.

    - (void) solvePuzzle:(id)sender{
        solveButton.enabled = NO;
        solveButton.title = @"Working . . . .";
    
        // I've tried using this as a Background thread, but I can't get the code to waitTilDone before continuing and changing the button state.
        [self performSelectorInBackground:@selector(createTreeFromNode:) withObject:rootNode];
    
        // NO - do not wait or enable here.
        // Need to wait here until createTreeFromNode is finished.
        solveButton.enabled=YES;
    
    }
    

    A UI message loop is running on the main thread which keeps the UI running. solvePuzzle is getting called on the main thread so you can’t wait – it will block the UI. It also can’t set the button back to enabled – the work hasn’t been done yet.

    It is the worker function’s job on the background thread to do the work and then when it’s done to then update the UI. But you cannot update the UI from a background thread. If you’re not using blocks and using performSelectInBackground, then when you’re done, call performSelectorOnMainThread which calls a selector to update your UI.

    performSelectorInBackground Sample:

    In this snippet, I have a button which invokes the long running work, a status label, and I added a slider to show I can move the slider while the bg work is done.

    // on click of button
    - (IBAction)doWork:(id)sender
    {
        [[self feedbackLabel] setText:@"Working ..."];
        [[self doWorkButton] setEnabled:NO];
    
        [self performSelectorInBackground:@selector(performLongRunningWork:) withObject:nil];
    }
    
    - (void)performLongRunningWork:(id)obj
    {
        // simulate 5 seconds of work
        // I added a slider to the form - I can slide it back and forth during the 5 sec.
        sleep(5);
        [self performSelectorOnMainThread:@selector(workDone:) withObject:nil waitUntilDone:YES];
    }
    
    - (void)workDone:(id)obj
    {
        [[self feedbackLabel] setText:@"Done ..."];
        [[self doWorkButton] setEnabled:YES];
    }
    

    GCD Sample:

    // on click of button
    - (IBAction)doWork:(id)sender
    {
        [[self feedbackLabel] setText:@"Working ..."];
        [[self doWorkButton] setEnabled:NO];
    
        // async queue for bg work
        // main queue for updating ui on main thread
        dispatch_queue_t queue = dispatch_queue_create("com.sample", 0);
        dispatch_queue_t main = dispatch_get_main_queue();
    
        //  do the long running work in bg async queue
        // within that, call to update UI on main thread.
        dispatch_async(queue, 
                       ^{ 
                           [self performLongRunningWork]; 
                           dispatch_async(main, ^{ [self workDone]; });
                       });    
    }
    
    - (void)performLongRunningWork
    {
        // simulate 5 seconds of work
        // I added a slider to the form - I can slide it back and forth during the 5 sec.
        sleep(5);
    }
    
    - (void)workDone
    {
        [[self feedbackLabel] setText:@"Done ..."];
        [[self doWorkButton] setEnabled:YES];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to loop through a bunch of documents I have to put
I have just tried to save a simple *.rtf file with some websites and

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.