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

Saturday, April 27, 2013

NSFileHandle readToEndOfFileInBackgroundAndNotify example ios


readToEndOfFileInBackgroundAndNotify

Reads to the end of file from the file or communications channel in the background and posts a notification when finished.
- (void)readToEndOfFileInBackgroundAndNotify
Discussion
This method performs an asynchronous readToEndOfFile operation on a file or communications channel and posts anNSFileHandleReadToEndOfFileCompletionNotification. You must call this method from a thread that has an active run loop.[NSFileHandle readToEndOfFileInBackgroundAndNotify]
The notification includes a userInfo dictionary that contains the data read; access this object using the NSFileHandleNotificationDataItem key.[NSFileHandle readToEndOfFileInBackgroundAndNotify]
Any object interested in receiving this data asynchronously must add itself as an observer of NSFileHandleReadToEndOfFileCompletionNotification. In communication via stream-type sockets, the receiver is often the object returned in the userInfo dictionary of NSFileHandleConnectionAcceptedNotification.
Example of [NSFileHandle readToEndOfFileInBackgroundAndNotify]

- (void)awakeFromNib
{
    NSFileHandle *remoteConnection = [NSFileHandle
fileHandleForReadingAtPath:@"/Users/user/Desktop/file.plist"];

    [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(readAllTheData:)
name:NSFileHandleReadToEndOfFileCompletionNotification
object:remoteConnection];
    [remoteConnection readToEndOfFileInBackgroundAndNotify];

}

- (void)readAllTheData:(NSNotification *)note {
    NSString *errors = nil;
    NSData *contentsOfDockFile = [note object];
    NSLog(@"%@", contentsOfDockFile);
    NSDictionary *testing = [NSPropertyListSerialization
propertyListFromData:contentsOfDockFile
mutabilityOption:NSPropertyListImmutable format:nil
errorDescription:&errors];
    NSLog(@"%@", testing);

    [[NSNotificationCenter defaultCenter] removeObserver:self
name:NSFileHandleReadToEndOfFileCompletionNotification object:[note
object]];

    [testing release];
}