Ios reads large files

Source: Internet
Author: User
Tags call back

Ios reads large files

// Read File Content operation-(void) loadFileContentsIntoTextView {// open a file through the stream NSInputStream * inputStream = [[NSInputStream alloc] initWithFileAtPath: filePath]; [inputStream open]; NSInteger maxLength = 128; uint8_t readBuffer [maxLength]; // whether BOOL endOfStreamReached = NO has been identified by the end; // NOTE: this tight loop will block until stream endswhile (! EndOfStreamReached) {NSInteger bytesRead = [inputStream read: readBuffer maxLength: maxLength]; if (bytesRead = 0) {// The file is read to the end of endOfStreamReached = YES ;} else if (bytesRead =-1) {// file read error endOfStreamReached = YES;} else {NSString * readBufferString = [[NSString alloc] initWithBytesNoCopy: readBuffer length: bytesRead encoding: NSUTF8StringEncoding freeWhenDone: NO]; // continuously loads characters to the view [self appendTextToView: readBufferString]; [readBufferString release] ;}} [inputStream close]; [inputStream release];}
 

Asynchronous File Reading: in terms of the network, the network reliability may cause the NSFileManager file operation method to be blocked, while the stream-based operations can achieve asynchronous reading. NSStream can work asynchronously. You can register a function for callback when the stream contains bytes that are readable. If there is no readable function, do not block it and call back it out.


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.