Showing posts with label waitForDataInBackgroundAndNotify example. Show all posts
Showing posts with label waitForDataInBackgroundAndNotify example. Show all posts

Saturday, April 27, 2013

NSFileHandle waitForDataInBackgroundAndNotify example ios


waitForDataInBackgroundAndNotify

Asynchronously checks to see if data is available.
- (void)waitForDataInBackgroundAndNotify
Discussion
When the data becomes available, this method posts a NSFileHandleDataAvailableNotification notification on the current thread.[NSFileHandle waitForDataInBackgroundAndNotify]
You must call this method from a thread that has an active run loop.
Example of [NSFileHandle waitForDataInBackgroundAndNotify]

/** * Fired whenever new data becomes available on STDERR * * @private */
-(void) errorData: (NSNotification *) notification { NSFileHandle *fileHandle = (NSFileHandle*) [notification object]; NSData *data = [fileHandle availableData]; if ([data length]) { // consume data // ... [fileHandle waitForDataInBackgroundAndNotify]; } else { // EOF was hit, remove observer [[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleDataAvailableNotification object:fileHandle]; } }

---------------------------------------------------------------------------

-(void) outData: (NSNotification *) notification
{
NSFileHandle *fileHandle = (NSFileHandle*) [notification object];
NSData *data = [fileHandle availableData];

if ([data length]) {

    NSString *currentString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    // do something 
}

[fileHandle waitForDataInBackgroundAndNotify]; //Checks to see if data is available in a background thread.
}


-(void) errData: (NSNotification *) notification
{
NSLog(@"errData");
NSFileHandle *fileHandle = (NSFileHandle*) [notification object];
[fileHandle waitForDataInBackgroundAndNotify];
}