【发布时间】:2012-10-01 12:58:42
【问题描述】:
我正在尝试在 Cbjective-C 中创建一个小型 rsync 程序。它目前通过 NSTask 访问终端命令行,并将命令行的输出读取到显示在 NSTextField 中的字符串;但是,当我在一个非常大的文件(大约 8 gb)上使用这个小程序时,它直到 RSYNC 完成后才会显示输出。我希望 NSTextField 在进程运行时不断更新。我有以下代码,正在寻找想法!:
-(IBAction)sync:(id)sender
{
NSString *sourcePath = self.source.stringValue;
NSString *destinationPath = self.destination.stringValue;
NSLog(@"The source is %@. The destination is %@.", sourcePath, destinationPath);
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/rsync"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-rptWav", @"--progress", sourcePath, destinationPath, nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
// [task setStandardInput:[NSPipe pipe]];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
while ([task isRunning])
{
NSString *readString;
readString = [[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding];
textView.string = readString;
NSLog(@"grep returned:\n%@", readString);
}
}
【问题讨论】:
-
您希望它更新什么?使用进度指示器肯定更有意义吗?
-
基本上 rsync 发生,并且在完成后输出出现在一个大批量字符串中。我想让字符串随着完成百分比等不断更新。 rsync 输出的
-
听起来更像是
rsync的问题,而不是文本字段的问题。您是否尝试过将--progress传递给rsync? -
是的,我使用 --progress 并且它可以工作,除了 rsync 的整个输出被放入一个文件中并且它不会被不断地读取,只有在最后一次
标签: objective-c macos command-line nstask