Skip to content

Instantly share code, notes, and snippets.

@kolyuchiy
Created October 25, 2013 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kolyuchiy/7152637 to your computer and use it in GitHub Desktop.
Save kolyuchiy/7152637 to your computer and use it in GitHub Desktop.
Read from file using dispatch_source
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