iOS reads large files

Source: Internet
Author: User

Read file contents operation-(void) loadfilecontentsintotextview{  //Open a file via stream nsinputstream *inputstream = [[Nsinputstream alloc] Initwithfileatpath:filepath]; [InputStream Open]; Nsinteger maxLength = 128;uint8_t Readbuffer [maxlength];//has been to the end to identify bool endofstreamreached = no;//Note:this tight loop would block until Stream Endswhile (! endofstreamreached) {Nsinteger bytesread = [InputStream read:readbuffer Maxlength:max Length];if (Bytesread = = 0) {//file read to last endofstreamreached = YES;} else if (bytesread = =-1) {//file read error endofstreamreached = YES;} Else{nsstring *readbufferstring =[[nsstring alloc] initwithbytesnocopy:readbuffer length:bytesread encoding: Nsutf8stringencoding Freewhendone:no];   

The asynchronous file reads, in the network aspect, because the network's unreliability may cause the Nsfilemanager file operation method the blocking, but the flow operation can realize the asynchronous reading. Nsstream can work asynchronously. You can register a callback function that has a byte-readable time in the stream, and if it is not readable, do not block the callback.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS reads large files

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.