iPhone SDK中如何实现NSThread在后台不断执行功能

发布于 2024-10-15 21:32:11 字数 274 浏览 6 评论 0原文

在我的 iPhone 应用程序中,我必须在后台不断执行功能。

为此,我认为我必须使用 NSThread 来调用该函数并使其在后台执行。

我不想停止我的应用程序,因此我想使用 NSThread 来保持我的主线程自由用于用户交互。

我应该如何实现 NSThread 以在后台执行该功能?

编辑:

该功能用于每 20 秒从 Web 服务器获取数据,并根据从 Web 服务器获取的数据更新我的 iPhone 应用程序中的表格。

In my iPhone app, I have to perform function constantly in background.

For that I think I will have to use NSThread to call the function and keep it executing in background.

I dont want to stall my app and hence I want to use NSThread to keep my Main Thread free for user interaction.

How should I implement NSThread to perform the function in background?

EDIT:

The function is for fetching the data from a web server every 20 seconds and updating the tables in my iPhone app based on the data that is fetched from the web server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅笑轻吟梦一曲 2024-10-22 21:32:11

我首先看一下 NSOperationQueue。

我猜你的后台任务实际上是一个一次又一次重复的小任务。将其创建为 NSOperation 子类并将它们添加到 NSOperationQueue 中。这样您就可以更轻松地控制后台任务。

您还可以使用 NSOperationQueue 获得优势,即当没有要运行的操作时,处理器不会陷入 while(YES) 循环中等待。这将有助于您的应用程序的 UI 响应更快,并有助于延长电池寿命 :)

但是,如果您的后台任务是一个单独的长时间运行的任务,只需启动然后忽略,那么 PerformSelectorInBackground 并不是一个太糟糕的主意。

I'd look at an NSOperationQueue first.

I'm guessing that your background task is really a small task repeated again and again. Make this into an NSOperation subclass and just add them onto an NSOperationQueue. That way you can control the background tasks more easily.

You also get the advantage with an NSOperationQueue that when there are no operations to run, the processor isn't just stuck in a while(YES) loop, waiting. This will help your app's UI be more responsive and will help battery life :)

However, if your background task is a single long running task that just needs to be started and then ignored, performSelectorInBackground isn't too bad an idea.

孤独岁月 2024-10-22 21:32:11

听起来是个坏主意,但它非常简单。

[self performSelectorInBackground:@selector(theMethod:) withObject:nil];

只需在方法中停留一段时间(是):它就永远不会停止执行。

编辑:

幸运的是,每 20 秒执行一次操作也同样简单。

[NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(theMethod:) userInfo:nil repeats:YES];

这将每 20 秒执行一次 theMethod: 一次。我还可以补充一点,这是一个更好的主意。

Sounds like a bad idea, but it's very simple.

[self performSelectorInBackground:@selector(theMethod:) withObject:nil];

Just have a while(YES) in theMethod: and it will never stop executing.

EDIT:

Luckily for you it's just as simple to do something once every 20 seconds.

[NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(theMethod:) userInfo:nil repeats:YES];

This will execute theMethod: once every 20 seconds. I might also add that this is a much better idea.

关于从前 2024-10-22 21:32:11

您需要与线程的运行循环进行交互。如果是 NSThread,则会自动创建。因此,您通常可以通过从辅助线程调用 + [NSThread currentRunLoop] 来访问 CF/NS-RunLoop。

在这种情况下,您可以将 CF/NS-Timer 添加到运行循环中,并让它运行并重复,直到您的工作完成。当计时器触发时,您的线程被唤醒,然后您就可以开始工作。

you'll want to interact with the thread's run loop. if it's an NSThread, it is created automatically. thus, you are given access to the CF/NS-RunLoop - typically by calling + [NSThread currentRunLoop] from the secondary thread.

in that case, you add a CF/NS-Timer to the run loop, and let it run and repeat until your work is finished. when the timer fires, your thread is awoken, and you then do your work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文