Any body have idea about getting notification from NSTask while NSTask is executed. I am unzipping a zip file using NSTask and need to show the unzip data progress in a NSProgressBar. I don't found any idea for doing such task.So that i show the value in progress bar. Need help for doing this task. Thanks in advance.
Use NSFileHandleReadCompletionNotification
, NSTaskDidTerminateNotification
notifications.
task=[[NSTask alloc] init];
[task setLaunchPath:Path];
NSPipe *outputpipe=[[NSPipe alloc]init];
NSPipe *errorpipe=[[NSPipe alloc]init];
NSFileHandle *output,*error;
[task setArguments: arguments];
[task setStandardOutput:outputpipe];
[task setStandardError:errorpipe];
output=[outputpipe fileHandleForReading];
error=[errorpipe fileHandleForReading];
[task launch];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedData:) name: NSFileHandleReadCompletionNotification object:output];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedError:) name: NSFileHandleReadCompletionNotification object:error];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TaskCompletion:) name: NSTaskDidTerminateNotification object:task];
//[input writeData:[NSMutableData initWithString:@"test"]];
[output readInBackgroundAndNotify];
[error readInBackgroundAndNotify];
[task waitUntilExit];
[outputpipe release];
[errorpipe release];
[task release];
[pool release];
/* Called when there is some data in the output pipe */
-(void) receivedData:(NSNotification*) rec_not
{
NSData *dataOutput=[[rec_not userInfo] objectForKey:NSFileHandleNotificationDataItem];
[[rec_not object] readInBackgroundAndNotify];
[strfromdata release];
}
/* Called when there is some data in the error pipe */
-(void) receivedError:(NSNotification*) rec_not
{
NSData *dataOutput=[[rec_not userInfo] objectForKey:NSFileHandleNotificationDataItem];
if( !dataOutput)
NSLog(@">>>>>>>>>>>>>>Empty Data");
[[rec_not object] readInBackgroundAndNotify];
}
/* Called when the task is complete */
-(void) TaskCompletion :(NSNotification*) rec_not
{
}
In order to show progress, you need to find out two things:
- How many files there are in the archive, or how many bytes they will occupy after unzipping is complete
- How many files or bytes you have unzipped so far
You'll find these out by reading the output from the unzip task. Parag Bafna's answer is a start; in receivedData:
, you'll need to parse the output to determine what progress has just happened, and then add that progress to your running count of progress so far (e.g., ++_filesUnzippedSoFar
).
The first part, finding out the total size of the job, is trickier. You basically need to run unzip before you run unzip: the first, with -l
(that's a lowercase L), is to List the contents of the archive; the second is to unzip it. The first one, you read the output to determine how many files/bytes the archive contains; the second one, you read the output to determine the value to advance the progress bar to.
Setting the properties of the progress bar is the easy part; those are literally just doubleValue
and maxValue
. Working out where you are in the job is the hard part, and is very domain-specific—you need to read unzip's output (twice, in different forms), understand what it's telling you, and translate that into progress information.
There is nothing in NSTask that can help you with that. NSTask's part of this begins and ends at the standardOutput
property. It has no knowledge of zip files, archives, contents of archives, or even progress, since none of that applies to most tasks. It's all specific to your task, which means you have to write the code to do it.
来源:https://stackoverflow.com/questions/17788267/get-notification-of-task-progress-from-nstask