iosnsinputstream

Can NSInputStream be used to read chunks of data with offset to be able to upload large files


I’d like to know if there is a way to use NSInputStream to read chunk of data with some offset. For example there is a file with size 100MB, I need chunks with size of 10MB but I need the 5th chunk before the 1st chunk.

The goal is to be able to upload large files to a server. The backend expects chunks of data, that can be sent at once. The idea is to use NSOperationQueue and to set maxConcurrentOperationCount to 5 for example. Each of the tasks will need different chunk to send. I know about - (NSData *)subdataWithRange:(NSRange)range - but this will require the whole NSData to be in the memory all the time. Which won’t work with large files, so I’d prefer to use NSInputStream. Can the chunks be read out of order with NSInputStream and how or should another approach be used?


Solution

  • You can open file-based NSInputStream and then specify required offset using NSStreamFileCurrentOffsetKey property. At the same time, in case of implementing upload upon NSURLRequiest with NSInputStream inside, the size of the chunk is not under your control. Its value hardcoded in CFNetworking framework.