• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Fwd: NSTask - working with asynchronous output with a finite termination time
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Fwd: NSTask - working with asynchronous output with a finite termination time


  • Subject: Fwd: NSTask - working with asynchronous output with a finite termination time
  • From: Matthew Brook <email@hidden>
  • Date: Sun, 7 Jan 2007 01:58:58 +0000



Dear All

I've been playing with NSTask and calling a task which returns data in an asynchronous fashion, but with a finite termination time.
The output is separated by \n (newlines), and I wanted to get it into an array.


I'm very pleased with myself because it works (finally!)

So I'm sharing and hope it saves someone else a headache or two.

If you have a task which is pretty near synchronous in its output then this is overkill.
It might need its own thread in some instances eg if you want to keep a UI active while it runs.


If anyone has any brighter ideas of how to do this, or comments I'd be grateful.

Thanks

Matt

NSTask *theTask = [[NSTask alloc] init];
NSPipe *thePipe = [[NSPipe alloc]init];
NSFileHandle *resultsFileHandle = [thePipe fileHandleForReading];
NSMutableString *resultsString = [[NSMutableString alloc] init];

[theTask setLaunchPath:@"/usr/bin/whateverTheTaskNameIs"];
[theTask setStandardOutput:thePipe];
[theTask setArguments:[NSArray arrayWithObjects:someStringOrOther,anotherString,nil]];
[theTask launch];

while ([theTask isRunning])
{
NSData *theData = [resultsFileHandle availableData];

if ([theData length])
{
NSString *newData = [[NSString alloc]
initWithData:theData
encoding:NSASCIIStringEncoding];

[resultsString appendString:newData];
[newData release];
}
}

if ([theTask terminationStatus]==0)
{
NSArray *resultsArray;
resultsArray = [resultsString componentsSeparatedByString:@"\n"];
//Note that the array has a final string object which has a value of "" which needs to be ignored
CFShow(resultsArray);

} else {
printf("Problem with NSTask call\n");
return (1);
}

[thePipe release];
[theTask release];


	//release other stuff




_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSTask - working with asynchronous output with a finite termination time
      • From: Nir Soffer <email@hidden>
  • Prev by Date: Help! NSFetchRequest/NSManagedObjectModel weirdness (or a memory problem)
  • Next by Date: Re: previousResponder?
  • Previous by thread: Help! NSFetchRequest/NSManagedObjectModel weirdness (or a memory problem)
  • Next by thread: Re: NSTask - working with asynchronous output with a finite termination time
  • Index(es):
    • Date
    • Thread