当后台任务完成时,如何在 iPhone 应用程序 IOS 程序中获得主 UI 线程的指示?
背景
您可以使用 -performSelectorOnMainThread:withObject:waitUntilDone:
从后台选择器回调到主线程,如下所示:
- (void)loadModel
{
// Load the model in the background
Model *aModel = /* load from some source */;
[self setModel:aModel];
[self performSelectorOnMainThreadselector(finishedLoadingModel) withObject:nil waitUntilDone:YES];
}
- (void)finishedLoadingModel
{
// Notify your view controller that the model has been loaded
[[self controller] modelLoaded:[self model]];
}
更新:更安全的方法是检查 -finishedLoadingModel
以确保您在主线程上运行:
- (void)finishedLoadingModel
{
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:_cmd withObject:nil waitUntilDone:YES];
}
// Notify your view controller that the model has been loaded
[[self controller] modelLoaded:[self model]];
}
关于iphone - 后台任务完成后如何指示主 UI 线程? (执行SelectorInBackground),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8031868/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |