我正在尝试在 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);
}
}