【发布时间】:2015-04-18 23:16:34
【问题描述】:
正如here 解释的那样,WKWebView 有一个错误,即捆绑本地网页的应用程序必须将捆绑包复制到tmp 目录中。我将捆绑包复制到tmp 的代码是:
// Clear tmp directory
NSArray* temporaryDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
for (NSString *file in temporaryDirectory) {
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"build"];
NSString *temporaryPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"build"];
NSError *error = nil;
// Copy directory
if(![fileManager copyItemAtPath:sourcePath toPath:temporaryPath error:&error]) {
[self logError:@"Could not copy directory" :error];
}
我已经为这个特定的代码片段计时,它占用了我应用程序启动时间的 50%! (总共约 1 秒中的约 0.5 秒。)
有没有一种方法可以在 iOS 8 下加速(或完全避免)这个特定的代码片段?线程有帮助吗?
【问题讨论】:
-
您是否尝试过将该代码放入后台队列?现在它看起来正在主线程上执行,这肯定会减慢启动速度。共享的
NSFileManager是线程安全的,所以你不应该在那里遇到任何问题。 -
这些文件的复制在我的应用程序的关键路径中。我的应用程序实际上是一个
WKWebView,需要尽快加载。 -
您要复制的捆绑包有多大?
-
您可以将图像处理为占用大部分空间的图像,并使其更小。但我会说 0.5 秒还不错,如果你将其降低到 0.2 或 0.1 秒,我认为不会很明显
标签: ios performance ios8 startup wkwebview