MKNetworkKit icon indicating copy to clipboard operation
MKNetworkKit copied to clipboard

Memory Management when using NSOutputStream in MKNetworkKit

Open johnganderson opened this issue 10 years ago • 0 comments

I have been in IT long enough to know most issues come down to me being too stupid (!) but: I want to download a media file from my JEE server (which I have full control over code etc). These files could be very large (video) hence need to be streamed. JEE side tested and works fine. On iOS side I am using MKNetworkKit and modelled my code on your blog example: [operation setDownloadStream:[NSOutputStream outputStreamToFileAtPath:@"/Users/mugunth/Desktop/DownloadedFile.pdf" append:YES]];

I actually create my NSOutputStream in a utility class and hand it to operation correcting setDownloadStream: to addDownloadStream:

self.outputStream = [VTMediaServices outputStreamInDocumentsDirectoryWithSubdirectoryName:subdirectoryName andFileName:imagefileName];

[op addDownloadStream:self.outputStream];

This only works (I'm in ARC) if I maintain a strong reference to outputStream! My issue is how to hold an unlimited (dynamically changeable) number of strong references to the outputStreams I am using and how to get notification I can release them.

I assumed I would be able to release (outputStream = nil;) in the SUCCESS BLOCK but this is called asynchronously and returns before the NSURLConnection is finished. When the NSURLConnection is finished you do an [outputStream close]; Hence this does not work!

I also assumed the fact the active MKNetworkOperation would be holding a strong reference to the NSOutputStream until it is finished and therefore ARC would release it once finished - but it does not seem to (as if I do not hold strong reference I only get first part of file downloaded).

I notice you are using NSURLConnectionDataDelegate instead of NSStreamDelegate. Do I need to use this to release (outputStream = nil;) my NSOutputStream?

I have spent last few days reading Apples manuals on all these classes and going thru your source code.

MY question is: how do you envisage users of MKNetworkKit managing memory of NSOutputStreams used for downloading files?

Any help gratefully received. Thanks in anticipation ...

johnganderson avatar Sep 03 '14 21:09 johnganderson