use*_*739 72 xcode multithreading multitasking ios shared-resource
我想知道如何function在主线程上调用我的.
如何确保function在主线程上调用我的?
(这是我之前的一个问题).
sho*_*123 162
这样做:
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
//Your code goes in here
NSLog(@"Main Thread Code");
}];
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
Car*_*rlJ 134
当您使用iOS> = 4时
dispatch_async(dispatch_get_main_queue(), ^{
//Your main thread code goes in here
NSLog(@"Im on the main thread");
});
Run Code Online (Sandbox Code Playgroud)
Amy*_*all 46
我可以遵循任何规则,以确保我的应用程序只在主线程中执行我自己的代码?
通常你不需要做任何事情来确保这一点 - 你的事情清单通常就足够了.除非您正在与某些API相互作用,而这些API恰好产生一个线程并在后台运行您的代码,否则您将在主线程上运行.
如果你想确定,你可以做的事情
[self performSelectorOnMainThread:@selector(myMethod:) withObject:anObj waitUntilDone:YES];
Run Code Online (Sandbox Code Playgroud)
在主线程上执行方法.(也有GCD等价物.)
Sea*_*ser 10
我认为这很酷,甚至通常它的好形式是让一个方法的调用者负责确保它被调用正确的线程.
if (![[NSThread currentThread] isMainThread]) {
[self performSelector:_cmd onThread:[NSThread mainThread] withObject:someObject waitUntilDone:NO];
return;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
109628 次 |
| 最近记录: |