开发者

How to update UI in a task completion block?

开发者 https://www.devze.com 2023-03-31 04:41 出处:网络
In my application, I let a progress indicator starts animation before I send a HTTP request. The completion handler is defined in a block. After I get the response data, I hide the 开发者_JAVA技巧prog

In my application, I let a progress indicator starts animation before I send a HTTP request. The completion handler is defined in a block. After I get the response data, I hide the 开发者_JAVA技巧progress indicator from inside the block. My question is, as I know, UI updates must be performed in the main thread. How can I make sure it?

If I define a method in the window controller which updates UI, and let the block calls the method instead of updating UI directly, is it a solution?


Also, if your app targets iOS >= 4 you can use Grand Central Dispatch:

dispatch_async(dispatch_get_main_queue(), ^{
    // This block will be executed asynchronously on the main thread.
});

This is useful when your custom logic cannot easily be expressed with the single selector and object arguments that the performSelect… methods take.

To execute a block synchronously, use dispatch_sync() – but make sure you’re not currently executing on the main queue or GCD will deadlock.

__block NSInteger alertResult; // The __block modifier makes alertResult writable
                               // from a referencing block.
void (^ getResponse)() = ^{
    NSAlert *alert = …;
    alertResult = [NSAlert runModal];
};

if ([NSThread isMainThread]) {
    // We're currently executing on the main thread.
    // We can execute the block directly.
    getResponse();
} else {
    dispatch_sync(dispatch_get_main_queue(), getResponse);
}

// Check the user response.
if (alertResult == …) {
    …
}


You probably misunderstood something. Using blocks doesn't mean that your code is running in a background thread. There are many plugins that work asynchronously (in another thread) and use blocks.

There are a few options to solve your problem.

You can check if your code is running in the main thread my using [NSThread isMainThread]. That helps you to make sure that you're not in the background.

You can also perform actions in the main or background by using performSelectorInMainThread:SEL or performSelectorInBackground:SEL.

The app immediately crashes when you're trying to call the UI from a bakcground thread so it's quite easy to find a bug.

0

精彩评论

暂无评论...
验证码 换一张
取 消