如何逐渐从NSTask检索数据

如何逐渐从NSTask检索数据

本文介绍了如何逐渐从NSTask检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为命令行工具使用GUI(Cocoa),以使人们更容易使用它.尽管它是GUI,但我想将输出显示到NSTextView.问题在于输出量很大,该工具执行的分析可能需要数小时/天.

I am working on a GUI (Cocoa) for a command-line tool to make it more accessible to people. Despite it being a GUI, I would like to display the output to an NSTextView. The problem is that the output is large and the analysis the tool carries out can take hours/days.

通常,在使用NSTask和NSPipe时,仅在任务完全完成(这可能需要很长时间)之后才显示输出.我要做的是将输出拆分并逐渐显示(例如,每分钟更新一次).

Normally, when working with NSTask and NSPipe, the output is displayed only after the task is completely finished (which can take a long time). What I want to do is split the output up and display it gradually (updating every minute for example).

到目前为止,我已经将数据处理放在单独的线程中:

So far I have placed the processing of the data in a separate thread:

[NSThread detachNewThreadSelector:@selector(processData:) toTarget:self withObject:raxmlHandle];

- (void)processData:(id)sender {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString *startString = [results string];
    NSString *newString = [[NSString alloc] initWithData:[raxmlHandle readDataToEndOfFile] encoding:NSASCIIStringEncoding];
    [results setString:[startString stringByAppendingString:newString]];
    [startString release];
    [newString release];
    [pool release];
}

对我来说,所有这一切仍然有些巫毒,我不确定如何应对这一挑战.

All this is still a bit of voodoo to me and I am not exactly sure how to deal with this challenge.

您有什么建议或建议吗?

Do you have any suggestions or recommendations?

谢谢!

推荐答案

您需要使用NSFileHandle提供的通知.

You need to use a notification provided by NSFileHandle.

首先,将您自己添加为 NSFileHandleReadCompletionNotification

First, add yourself as an observer to the NSFileHandleReadCompletionNotification

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(outputReceived:)
                                             name:NSFileHandleReadCompletionNotification
                                           object:nil];

然后,准备任务,调用 readInBackgrounAndNotify 管道的文件句柄,然后启动任务.

Then, prepare a task, call readInBackgrounAndNotify of the file handle of the pipe, and launch the task.

NSTask*task=[[NSTask alloc] init];
[task setLaunchPath:...];

NSPipe*pipe=[NSPipe pipe];
[task setStandardOutput:pipe];
[task setStandardError:pipe];

// this causes the notification to be fired when the data is available
[[pipe fileHandleForReading] readInBackgroundAndNotify];

[task launch];

现在,要实际接收数据,您需要定义一个方法

Now, to actually receive the data, you need to define a method

-(void)outputReceived:(NSNotification*)notification{
    NSFileHandle*fh=[notification object];
    // it might be good to check that this file handle is the one you want to read
    ...

    NSData*d=[[aNotification userInfo] objectForKey:@"NSFileHandleNotificationDataItem"];
    ... do something with data ...
}

您可能想阅读通知编程主题以了解正在发生的情况.

You might want to read Notification Programming Topics to understand what is going on.

这篇关于如何逐渐从NSTask检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:18