iOS - 确保在主线程上执行 [英] iOS - Ensure execution on main thread
问题描述
我想知道如何在主线程上调用我的函数
。
I want to know how to call my function
on the main thread.
如何确保在主线程上调用函数
?
(这是我之前的问题)。
推荐答案
我可以遵循任何规则以确保我的应用程序执行我自己的代码只是在主线程中?
there any rule I can follow to be sure that my app executes my own code just in the main thread?
通常你不需要做任何事情来确保这一点 - 你的事情清单通常就足够了。除非您正在与某些API进行交互,而这些API恰好产生一个线程并在后台运行您的代码,否则您将在主线程上运行。
Typically you wouldn't need to do anything to ensure this — your list of things is usually enough. Unless you're interacting with some API that happens to spawn a thread and run your code in the background, you'll be running on the main thread.
如果您需要要确定,你可以做一些事情,比如
If you want to be really sure, you can do things like
[self performSelectorOnMainThread:@selector(myMethod:) withObject:anObj waitUntilDone:YES];
在主线程上执行方法。 (也有GCD等价物。)
to execute a method on the main thread. (There's a GCD equivalent too.)
这篇关于iOS - 确保在主线程上执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!