gpt4 book ai didi

objective-c - 如何优化压缩和上传文件的循环

转载 作者:行者123 更新时间:2023-12-03 17:15:58 24 4
gpt4 key购买 nike

我正在循环访问由核心数据谓词检索的文件路径数组,在循环的每次迭代中压缩和上传每个文件,总共大约 4 或 5000 个文件。 (我用于压缩的技术基本上就是此处描述的技术: Best way to loop through a lot of files and zip each one separately? )目前,我正在 Xcode 中以 Debug模式运行该应用程序。过了一会儿,随着循环的每次迭代,不仅应用程序似乎运行得更慢,Xcode 也是如此 - 事实上,我的整个计算机的响应速度变得越来越慢。我现在正在研究涉及 NSStrings、NSArrays、NSNotifications 等的有效循环的一般规则,但我也希望有人也能指出我的代码中明显的瓶颈和低效率。尽管我的代码使用垃圾收集,但我尝试放入发布调用,希望可能会有所作为,尽管我的理解是它不应该。这是我的代码的删节版本,仅包含重要部分:

//Zip paths saved in Core Data and upload them to a server
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

//get path of the shell script that zips a file
NSString *zipShellScript = [[NSBundle mainBundle] pathForResource:@"backupzipper"
ofType:@"sh"];

//get array of File managed objects related to User managed object
NSArray *filesToUpload = [User files];

//args for the shell script
//NSString *sourceFilePath = [NSString string];
//NSString *targetFilename = [NSString string];
//NSString *targetFilePath = [NSString string];

//the following appears to make a big improvement
NSString *sourceFilePath = [[NSString alloc] initWithString:[NSString string]];
NSString *targetFileName = [[NSString alloc] initWithString:[NSString string]];
NSString *targetFilePath = [[NSString alloc] initWithString:[NSString string]];

NSError *error;

for (File *file in filesToUpload) //loop through array of File managed objects
{
NSTask *task = [[NSTask alloc] init];

sourceFilePath = file.path;
targetFilename = [NSString stringWithFormat:@"%@.zip",
[[sourceFilePath lastPathComponent]
stringByDeletingPathExtension]]; //e.g. misc.doc will produce misc.zip

[task setArguemnts:[NSArray arrayWithObjects:zipShellScript,
sourceFilePath, //full path of file to zip e.g. /Users/stifin/documents/misc.doc
workingDirectory, //where the zip will be placed e.g. /Users/stifin/Library/Application Support/MyApp/
targetFilename, //filename after zipping e.g. misc.zip
nil]];
[sourceFilePath release]; //my attempt to reduce memory usage even though this is garbage collected app
[targetFilename release];

[task setLaunchPath@"/bin/sh"];
[task setStandardInput:[NSPipe pipe]]; //fixes odd behaviour where NSLog no longer works after NSTask

//do zip
[task launch];
[task waitUntilExit];

if ([task terminationReason] == NSTaskTerminationReasonExit)
{
[task release]; //doubt this helps

[self uploadFile:targetFilePath]; //method just sleeps for 1 sec to simulate upload time

file.dateUploaded = [NSDate date];
error = nil;
if ([context save:&error])
NSLog(@"Saved ok");
else
NSLog(@"Save error: %@", [error localizedDescription]);

//delete zip from working directory
[self cleanUpFile:targetFilePath];
[targetFilePath release]; //doubt this helps

//send notification of file processed
info = [NSDictionary dictionaryWithObjectsAndKeys:
file.filename, @"name",
file.sizeBytes, @"size",
[NSNumber numberWithBool:YES], @"success",
nil];
[nc postnNotificationName:@"FileProcessed" object:nil userInfo:info];
[info release]; //doubt this helps
}
else {
//handle task failure
}
}
[nc removeObserver:self];

最佳答案

正如 documentation 所指出的, -release 被忽略。如果您想立即收集,请使用[[NSGarbageCollector defaultCollector]collectIfNeeded];,甚至-collectExhaustively。不要在每次循环中都执行此操作,只需每隔大约一百次就执行此操作。

当然,所有这些都假设您已经使用 Instruments 来确保这就是问题所在。你没有说你是否这样做了,这让我相信你在做出假设。问题可能只是在其他地方(尽管我同意这是迄今为止最有可能的候选者 - GC + 紧密循环中的许多分配需要额外小心)。这不是一个好的工作方式。先测量,然后再优化。

关于objective-c - 如何优化压缩和上传文件的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6693948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com