How to update UI in a task completion block?

浪子不回头ぞ 提交于 2019-12-06 05:54:33

问题


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 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?


回答1:


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 == …) {
    …
}



回答2:


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.



来源:https://stackoverflow.com/questions/7205547/how-to-update-ui-in-a-task-completion-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!