Created
October 25, 2013 10:29
-
-
Save kolyuchiy/7152637 to your computer and use it in GitHub Desktop.
Read from file using dispatch_source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int rc = 0; | |
void *opaque = myfile_alloc(); | |
const char *incomplete_movie_filename = [[self.incompleteMovieAsset.movieURL path] cStringUsingEncoding:NSASCIIStringEncoding]; | |
rc = myfile_open(opaque, incomplete_movie_filename); | |
if (rc < 0) { | |
if (error) *error = [NSError errorWithFFStatus:rc]; | |
return NO; | |
} | |
// | |
int fd = myfile_fd(opaque); | |
fcntl(fd, F_SETFL, O_NONBLOCK); // Avoid blocking the read operation | |
dispatch_queue_t queue = dispatch_queue_create("myfile-io", DISPATCH_QUEUE_SERIAL); | |
dispatch_source_t readSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, | |
fd, 0, queue); | |
if (readSource == nil) { | |
return NO; | |
} | |
dispatch_source_set_cancel_handler(readSource, ^{ | |
myfile_close(opaque); | |
myfile_dealloc(&opaque); | |
}); | |
dispatch_source_set_event_handler(readSource, ^{ | |
unsigned long bytes_available = dispatch_source_get_data(readSource); | |
// Now we can start reading... | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment