1

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.

4

2 回答 2

3

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];
于 2009-06-25T07:07:45.877 回答
0

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

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

NSOperation 是一种提供强大线程操作的非常简单的方法——您可以选择在此过程中使用您想要执行的任何更新来回调主线程。

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

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

于 2009-06-25T07:11:15.010 回答