2

how can i update GUI elements with values from a queue? if i use async queue construct, textlable don't get updated. Here is a code example i use:

- (IBAction)dbSizeButton:(id)sender {
    dispatch_queue_t getDbSize = dispatch_queue_create("getDbSize", NULL);
    dispatch_async(getDbSize, ^(void)
    {
        [_dbsizeLable setText:[dbmanager getDbSize]]; 
    });

   dispatch_release(getDbSize);
}

Thank you.

1
  • Did you try performSelectorOnMainThread:withObject:waitUntilDone:? Commented Jan 3, 2012 at 20:27

2 Answers 2

9

As @MarkGranoff said, all UI needs to be handled on the main thread. You could do it with performSelectorOnMainThread, but with GCD it would be something like this:

- (IBAction)dbSizeButton:(id)sender {

    dispatch_queue_t getDbSize = dispatch_queue_create("getDbSize", NULL);
    dispatch_queue_t main = dispatch_get_main_queue();
    dispatch_async(getDbSize, ^(void)
    {
        dispatch_async(main, ^{ 
            [_dbsizeLable setText:[dbmanager getDbSize]];
        });
    });

    // release
}   
Sign up to request clarification or add additional context in comments.

3 Comments

Except you'd probably want the -getDbSize occurring on the private queue, not the main queue...
@Catfish_Man: But isn't getDbSize the private queue?
It's not happening on the getDbSize queue. It's happening in a dispatch_async'd block running on the main queue. You need it inside the first dispatch_async, but outside the second.
2

Any UI update must be performed on the main thread. So your code would need to modified to use the main dispatch queue, not a queue of your own creation. Or, any of the performSelectorOnMainThread methods would work as well. (But GCD is the way to go, these days!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.