Saturday, April 27, 2013

NSFileHandle waitForDataInBackgroundAndNotifyForModes example ios


waitForDataInBackgroundAndNotifyForModes:

Asynchronously checks to see if data is available.
- (void)waitForDataInBackgroundAndNotifyForModes:(NSArray *)modes
Parameters
modes
The runloop modes in which the data available notification can be posted.
Discussion of [NSFileHandle waitForDataInBackgroundAndNotifyForModes]
When the data becomes available, this method posts a NSFileHandleDataAvailableNotification notification on the current thread. This method differs from waitForDataInBackgroundAndNotify in that modes specifies the run-loop mode (or modes) in which NSFileHandleDataAvailableNotification can be posted. [NSFileHandle waitForDataInBackgroundAndNotifyForModes]
You must call this method from a thread that has an active run loop.
Example of [NSFileHandle waitForDataInBackgroundAndNotifyForModes]
-(void) errorData: (NSNotification *) notification
{ NSFileHandle *fileHandle = (NSFileHandle*) [notification object]; NSData *data = [fileHandle availableData]; if ([data length]) { // consume data // ... [fileHandle waitForDataInBackgroundAndNotifyForModes:[NSArray arrayWithObject:NSDefaultRunLoopMode]];
} else { // EOF was hit, remove observer [[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleDataAvailableNotification object:fileHandle]; } }