在 iPhone 应用程序中使用睡眠(尤其是使用 UINavigationController)

发布于 2024-07-25 05:48:23 字数 781 浏览 4 评论 0原文

我对 iPhone 开发还很陌生,但我即将发布我的第一个应用程序(与我运行的网站相关)。 该应用程序需要一个非常大的数据库,因此我决定仅在本地存储最常用的数据,通过 JSON Web 服务调用从我的网站运行的数据库检索其他数据。

虽然使用模拟器执行正常(点击实时数据库)依赖于网络服务调用的搜索所花费的时间比我在手机上运行时所希望的要长。 与即时的本机搜索相比,这些调用看起来要糟糕得多。 为了减少相对差异,我想为本机搜索添加一个假插页式广告(带有活动指示器的页面)(网络服务搜索已经使用了插页式广告),但我一直在与导航控制器推送时间结合使用时遇到问题睡觉(n)。

无论如何,我的应用程序的搜索部分是一个 tabController 选项卡中的 navController。 当尝试使用这样的代码时:

[[tabBarController.viewControllers objectAtIndex:0] pushViewController:(UIViewController *)waitingController animated:YES];
sleep(2);

我发现推送总是等待睡眠完成后再执行,而我想要的效果是推送视图控制器,然后应用程序等待两秒钟,然后继续模拟搜索过程。

我在 navController 推送中遇到了一些其他奇怪的结果,有几次,当我作为插页式推送的视图控制器在内容方面仍然保留在适当位置时,我经历了似乎是两个单独的视图控制器的混合,只有标题我想要将其推入其剩余位置的那个。

我确信我根本上缺乏理解,所以我希望在这里得到一些指导。

干杯,

艾伦。

I'm pretty new to iPhone development but I'm close to releasing my first app (related to a website I run). The app requires a very large database and as such I've decided to store only the most commonly used data locally, retrieving the other data via a JSON web service call from the database my website runs off.

Whilst performing OK using the simulator (hitting the live DB) searches relying on the web service call have been taking longer than I'd have hoped they would when running on the phone. These calls look much worse when compared to the native searches which are instantaneous. To reduce the relative difference I wanted to put in a fake interstitial (page with activity indicator) for the native searches (the web service searches already use one) but I've been having an issue with the timing of navigation controller pushes when combined with sleep(n).

Anyway, the search section of my app is a navController wihin a tabController tab. When trying to use code like this:

[[tabBarController.viewControllers objectAtIndex:0] pushViewController:(UIViewController *)waitingController animated:YES];
sleep(2);

I find that the push always waits for the sleep to finish before executing when the effect I want is for the viewcontroller to be pushed and then the app to wait for two seconds before continuing to simulate the search process.

I've had some other weird results with navController pushes, on a few occasions I've experienced what seems to be a mashing of two separate viewcontrollers when the one I've pushed as interstitial remains in place content-wise with only the title of the one I want pushed in it's place remaining.

I'm sure that there is a fundamental lack of understanding on my part to blame so I'm hoping for a bit of guidance here.

Cheers,

Alan.

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

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

发布评论

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

评论(2

尹雨沫 2024-08-01 05:48:23

sleep(float) 阻塞主线程,导致 UI 冻结。 您应该使用 -[NSObject PerformSelector:withObject:afterDelay:]

示例:

[label performSelector:@selector(setText:) withObject:@"Delayed Hello World!" afterDelay:2.0f];

sleep(float) blocks the main thread causing the UI to freeze up. You should instead schedule some action to be performed later using -[NSObject performSelector:withObject:afterDelay:]

Example:

[label performSelector:@selector(setText:) withObject:@"Delayed Hello World!" afterDelay:2.0f];
负佳期 2024-08-01 05:48:23

我会考虑改变你的架构,而不是在主线程上睡觉——这不是一个很好的用户体验!

考虑使用 NSOperation 来控制与 Web 服务的交互 - 这将允许您对操作进行排队并让它们按特定顺序或并行运行。

NSOperation 是提供健壮线程操作的一种非常简单的方法 - 您可以选择回调主线程,并在此过程中进行任何更新。

我认为这种架构将帮助您使用户界面变得更好 - 但是一旦您重新配置了您的思维以使用操作,您就会发现许多其他好处。

注意:我花了好几次才真正掌握 NSOperation 和 NSOperationQueue - 但我投入的时间绝对是值得的。

I would look at changing your architecture and not sleeping on the main thread - not a great user experience!

Look into using NSOperation to control your interactions with the web service - this will allow you to queue up operations and have them run in a specific sequence or in parallel.

NSOperation is a very easy way to provide robust threading operations - and you could choose to make a call back to the main thread with any updates you want to do along the way.

I think this architecture will help you make the user interface much better - but once you have reconfigured your thinking to use operations you will find lots of other benefits.

NB: It took me a few times to really get to grips with NSOperation and NSOperationQueue - but the time I invested was definitely worth it.

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