NSRunloops和强制事件处理 [英] NSRunloops and forcing event processing

查看:144
本文介绍了NSRunloops和强制事件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iPhone应用程序中有几个选项卡需要几秒钟的时间(从本地sqlite数据库中提取大量的数据)。当用户触摸标签时,它看起来好像应用程序什么也不做。我试图把一个窗口显示一个旋转器,但它从来没有显示,由于事实,处理紧接着。

I have a few tabs in my iPhone application which take a few seconds to load (pulling large amounts of data from a local sqlite database). When the users touch the tabs it appears as if the application is doing absolutely nothing. I've attempted to put a window showing a spinner up, however it is never shown due to the fact that the processing immediately follows.

我知道我有几个不同的选项来异步加载数据,但是我想轮询社区,看看是否有任何潜在的问题,只是强迫另一个周期

I know i have a couple of different options for loading the data asynchronously, however i wanted to poll the community and see if there were any potential problems with just forcing another cycle of the NSRunloop to show the window.

这是我的代码看起来像...

Here's what my code looks like...

[[ActivityIndicator sharedActivityIndicator] show];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]];

在1到10的范围内,您对此黑客的评价有多糟?

On a scale of 1 to 10 how bad would you rate this hack?

推荐答案

我不知道我会在哪里评价,但我知道我不想这样做。与系统默认runloop的混乱只是一个坏主意。

I don't know where I'd rate it, but I know I wouldn't want to do it that way. Messing with the system default runloop just seems like a bad idea.

有几个我认为是好的办法。最简单的是将额外的处理放在一个单独的私有方法,然后这样做:

There are a couple of what I think are good approaches. The simplest is to put the additional processing in a separate private method, and then do this:

[[ActivityIndicator sharedActivityIndicator] show];
[self performSelector:@selector(processingMethod) withObject:nil afterDelay:0];

这将导致在您的指标显示后,在运行循环结束时调用processingMethod。应该工作得很好。

That will cause processingMethod to be called at the end of the run loop, after your indicator is showing. Should work fine.

一个警告是,如果你的指标是动画,根据它的设置,它可能不是动画,而processingMethod运行。在这种情况下,你想要在后台线程中运行processingMethod,这可能会更复杂一些,或者可以简单到这样做:

The one caveat is that if your indicator is animated, depending on how it's set up it may not be animated while processingMethod is running. In that case you'd want to run processingMethod in a background thread, which might be a little more complicated, or might be as simple as doing this instead:

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

潜在的复杂性是,在处理结束时方法,当你要显示结果的处理,你可能需要调用一个方法回到主线程。

The potential complication there is that at the end of processingMethod, when you're going to display the results of your processing, you may have to call a method back onto the main thread.

这篇关于NSRunloops和强制事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆