the*_*aws 11 iphone objective-c
如何确保我的函数仅在主线程上运行?(它更新UI元素)
这样的函数被认为是"坏"吗?
-(void)updateSomethingOnMainThread {
if ( ![[NSThread currentThread] isEqual:[NSThread mainThread]] )
[self performSelectorOnMainThread:_cmd withObject:nil waitUntilDone:NO];
else {
// Do stuff on main thread
}
}
Run Code Online (Sandbox Code Playgroud)
我这样写它是为了避免有第二个功能,最初我有这样的:
-(void)updateSomethingOnMainThread_real {
// Do stuff on main thread
}
-(void)updateSomethingOnMainThread {
[self performSelectorOnMainThread:@selector(updateSomethingOnMainThread_real) withObject:nil waitUntilDone:NO];
}
Run Code Online (Sandbox Code Playgroud)
Bra*_*son 15
作为ayoy基于方法的GCD实现的替代方案,用于保证在主线程上的执行,我在我的代码中使用了以下基于GCD的函数(来自我的另一个答案):
void runOnMainThreadWithoutDeadlocking(void (^block)(void))
{
if ([NSThread isMainThread])
{
block();
}
else
{
dispatch_sync(dispatch_get_main_queue(), block);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在代码中的任何位置使用此辅助函数:
runOnMainThreadWithoutDeadlocking(^{
// Do stuff that needs to be on the main thread
});
Run Code Online (Sandbox Code Playgroud)
这保证了封闭块中的操作将始终在主线程上运行,无论哪个线程调用它.它添加了很少的代码,并且相当明确地说明需要在主线程上运行哪些代码.
我写了这个简单的#define,我一直在使用它非常成功:
#define ensureInMainThread(); if (!NSThread.isMainThread) { [self performSelectorOnMainThread:_cmd withObject:nil waitUntilDone:NO]; return; }
Run Code Online (Sandbox Code Playgroud)
这样你的方法,假设它是无参数的,看起来像这样
- (void) updateTheThings {
ensureInMainThread();
[self.dog setTailWag:YES];
// etc...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14149 次 |
| 最近记录: |