• 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
Re: Reading data from an NSTask
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading data from an NSTask


  • Subject: Re: Reading data from an NSTask
  • From: Ken Tozier <email@hidden>
  • Date: Mon, 4 Jul 2005 13:47:01 -0400


On Jul 4, 2005, at 1:06 PM, j o a r wrote:


On 4 jul 2005, at 18.50, Ken Tozier wrote:


I created an NSTask to run nibtool and am seeing the result in the run log, but so far haven't had any luck at pulling the data out of the task. Anyone see the problem in the below snippet?

NSTask *testTask1 = [NSTask launchedTaskWithLaunchPath: @"/usr/bin/nibtool"
arguments: [NSArray arrayWithObjects: @"-a", @"/Users/kentozier/Desktop/TestNib.nib", nil]];


// if I step through in the debugger, it hangs on the next line.
// The run log however shows that the nib data exists somewhere as it get printed.
NSData *test1Data = [[testTask1 standardOutput] readDataToEndOfFile];


NSString *testString = [[NSString alloc] initWithData: test1Data
encoding: NSUnicodeStringEncoding];


// the code never seems to make it to here
NSLog(@"standard output = %@", testString);


The problem is probably that you don't empty the buffer in the queue between the processes.

Not sure I follow. Isn't the newly allocated pipe empty, and filled up by the launchedTaskWithLaunchPath?


When the buffer is full it just hangs, waiting for you to make more space until it can continue. This is something of a FAQ, something should be added to the standard documentation to better explain that this can happen, and a suggested implementation for handling it. There is information about this in the list archives, but it can be a bit tricky to find. Perhaps this will do:

<http://www.cocoabuilder.com/archive/message/cocoa/2003/12/29/78363>

I took a look which resulted in the following rewrite:

NSTask        *testTask    = [[NSTask alloc] init];

[testTask setArguments: [NSArray arrayWithObjects: @"-a", @"/User/ kentozier/Desktop/TestNib.nib", nil]];
[testTask setLaunchPath: @"/usr/bin/nibtool"];
[testTask setStandardOutput: [NSPipe pipe]];
[testTask launch];
[testTask waitUntilExit];
NSData *taskData = [[[testTask standardOutput] fileHandleForReading] availableData];
NSString *taskDataString = [[NSString alloc] initWithData: taskData
encoding: NSUnicodeStringEncoding];


NSLog(@"taskDataString = %@", taskDataString);

But I'm still seeing the same thing. All the data appears in the run log but "taskData = [[[testTask standardOutput] fileHandleForReading] availableData]" is empty. It executes OK but there's nothing in it.

Any ideas?

Thanks

Ken
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Reading data from an NSTask
      • From: j o a r <email@hidden>
References: 
 >Reading data from an NSTask (From: Ken Tozier <email@hidden>)
 >Re: Reading data from an NSTask (From: j o a r <email@hidden>)

  • Prev by Date: Re: Why does app sometimes bounce in the Dock but never finish loading?
  • Next by Date: Linking libxml and libxslt
  • Previous by thread: Re: Reading data from an NSTask
  • Next by thread: Re: Reading data from an NSTask
  • Index(es):
    • Date
    • Thread