如何加快 iPad 版应用程序的速度?

发布于 2024-09-06 19:25:51 字数 330 浏览 9 评论 0原文

Application Specific Information:
com.oneorangetree.iphoneexample failed to launch in time 
elapsed total CPU time (seconds): 3.500 (user 1.680, system 1.820), 17% CPU 
elapsed application CPU time (seconds): 0.920, 5% CPU

当应用程序加载时,它会在 viewDidLoad 中执行很多操作 大约需要 30 秒才能完成。我怎样才能把它放在后台线程或其他东西来加速应用程序并防止崩溃(因为加载时间太长)

Application Specific Information:
com.oneorangetree.iphoneexample failed to launch in time 
elapsed total CPU time (seconds): 3.500 (user 1.680, system 1.820), 17% CPU 
elapsed application CPU time (seconds): 0.920, 5% CPU

When the app loads, it does ALOT of stuff in viewDidLoad
It takes about 30 seconds to complete. How can I put this in a background thread or something to speed up the app and prevent the crash (because it took too long to load)

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

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

发布评论

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

评论(2

我爱人 2024-09-13 19:25:52

我过去曾使用 NSOperation/NSOperationQueue 进行简单的线程处理。具体来说,NSInitationOperation 使旋转需要一段时间的方法调用变得非常容易,而 NSOperationQueue 实际上为您线程化它。您生成的方法必须是线程安全的,但这并不是特别难做到。例如,您可以在 -init-viewDidLoad 方法中创建一个 NSOperationQueue,然后添加 NSInitationOperation到队列中并按其方式发送。

NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
NSInvocationOperation *lengthyTask = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(processAddresses) object:nil];
[opQueue addOperation:lengthyTask];
[lengthyTask release];

一件很酷的事情(尤其是在桌面上)是在 10.6(和 iOS 4)上自动使用 Grand Central Dispatch。

虽然线程化一个冗长的任务应该会让你的应用程序响应更快(特别是如果你观察结果并在它们来自线程化任务时显示它们,也许使用 KVO),但实现一些缓存将是有益的。每次启动时重新加载地址簿的成本会非常高,尤其是大多数人不会经常更改他们的地址簿。您可以将计算出的数据存储在本地文件或数据库中(Core Data 不太难使用,如果太慢可以直接使用 SQLite)。然后在启动时,您可以运行地址簿,比较上次运行应用程序时每条记录的修改日期,对新修改的记录进行地理编码。

I've used NSOperation/NSOperationQueue in the past for simple threading. Specifically, NSInvocationOperation makes it really easy to spin a method call that takes a while off, and NSOperationQueue practically threads it for you. The method you're spawning off has to be thread safe, but that is not particularly hard to do. For example, you could create an NSOperationQueue in your -init or -viewDidLoad methods, and then add the NSInvocationOperation to the queue and send it on it's way.

NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
NSInvocationOperation *lengthyTask = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(processAddresses) object:nil];
[opQueue addOperation:lengthyTask];
[lengthyTask release];

One of the kinda cool things (especially on the desktop) is that on 10.6 (and iOS 4) Grand Central Dispatch is automatically used.

While threading a lengthy task should make your app be more responsive (especially if you watch the results and display them as they come in from the threaded task, maybe with KVO), it would be beneficial to implement some caching. Reloading the address book every launch would be very costly, especially most people don't change their address books a lot. You could store the computed data in a local file or database (Core Data is not too hard to use, and if it's too slow you can use SQLite directly). Then on launch, you can run through the address book, comparing the modification dates on each record from the last time your app was run, geocoding the newly modified records.

荒人说梦 2024-09-13 19:25:52

是的,您可以使用后台线程,但是更好的方法是在可能的情况下预先计算(或缓存)事物,以便加载时间不会那么长。
您的代码正在执行哪些操作,需要很长时间才能运行?

Yes, you could use a background thread, however a better approach would be to pre-calculate (or cache) things where possible so the loading time is not as long.
What sort of things is your code doing that takes so long to run?

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