@@ -41,7 +41,8 @@ - (void) onOriginalQueue:(void(^)(void))aBlock;
4141@implementation IRRemoteResourceDownloadOperation
4242
4343@synthesize path, mimeType, url, processedBytes, totalBytes, preferredByteOffset, executing, finished, cancelled;
44- @synthesize fileHandle, connection;
44+ @synthesize fileHandle = _fileHandle;
45+ @synthesize connection = _connection;
4546@synthesize actualDispatchQueue;
4647@synthesize onMain;
4748@synthesize appendedCompletionBlocks;
@@ -52,34 +53,21 @@ + (IRRemoteResourceDownloadOperation *) operationWithURL:(NSURL *)aRemoteURL pat
5253 NSParameterAssert (aRemoteURL);
5354 NSParameterAssert (aLocalPath);
5455
55- IRRemoteResourceDownloadOperation *returnedOperation = [[[ self alloc ] init ] autorelease ];
56+ IRRemoteResourceDownloadOperation *returnedOperation = [[self alloc ] init ];
5657
5758 returnedOperation.url = aRemoteURL;
5859 returnedOperation.path = aLocalPath;
5960 returnedOperation.onMain = aPrelude;
6061 returnedOperation.completionBlock = aBlock;
62+
6163 return returnedOperation;
6264
6365}
6466
6567- (void ) dealloc {
6668
67- [mimeType release ];
68-
69- [path release ];
70- [url release ];
71- [fileHandle release ];
72-
73- __block NSURLConnection *nrConnection = connection;
74- dispatch_async (dispatch_get_main_queue (), ^ {
75- [nrConnection release ];
76- });
77-
78- [onMain release ];
79- [appendedCompletionBlocks release ];
69+ [_connection cancel ];
8070
81- [super dealloc ];
82-
8371}
8472
8573- (void ) onMainQueue : (void (^)(void ))aBlock {
@@ -135,18 +123,19 @@ - (void) main {
135123
136124 [self onMainQueue: ^ {
137125
138- NSMutableURLRequest *usedRequest = [NSMutableURLRequest requestWithURL: self .url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10 ];
139- NSURLConnection *usedConnection = [[[NSURLConnection alloc ] initWithRequest: usedRequest delegate: self startImmediately: NO ] autorelease ];
126+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: self .url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10 ];
127+
128+ NSURLConnection *connection = [[NSURLConnection alloc ] initWithRequest: request delegate: self startImmediately: NO ];
140129
141- objc_setAssociatedObject (usedConnection , &kIRRemoteResourcesDownloadOperation_connectionRequest , usedRequest , OBJC_ASSOCIATION_RETAIN_NONATOMIC );
142- self.connection = usedConnection ;
130+ objc_setAssociatedObject (connection , &kIRRemoteResourcesDownloadOperation_connectionRequest , request , OBJC_ASSOCIATION_RETAIN_NONATOMIC );
131+ self.connection = connection ;
143132
144133 [self .delegate remoteResourceDownloadOperationWillBegin: self ];
145134
146- usedConnection = [[[ NSURLConnection alloc ] initWithRequest: usedRequest delegate: self startImmediately: NO ] autorelease ];
135+ connection = [[NSURLConnection alloc ] initWithRequest: request delegate: self startImmediately: NO ];
147136
148- objc_setAssociatedObject (usedConnection , &kIRRemoteResourcesDownloadOperation_connectionRequest , usedRequest , OBJC_ASSOCIATION_RETAIN_NONATOMIC );
149- self.connection = usedConnection ;
137+ objc_setAssociatedObject (connection , &kIRRemoteResourcesDownloadOperation_connectionRequest , request , OBJC_ASSOCIATION_RETAIN_NONATOMIC );
138+ self.connection = connection ;
150139
151140 // Do not mutate the URL
152141 // self.url = usedRequest.URL;
@@ -248,13 +237,16 @@ - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
248237
249238 // If there is a MIME type available, rename the underlying file
250239
251- NSFileManager *fm = [NSFileManager defaultManager ];
240+ CFStringRef const tag = (__bridge CFStringRef)self.mimeType ;
241+ NSString *utiType = (NSString *)CFBridgingRelease (UTTypeCreatePreferredIdentifierForTag (kUTTagClassMIMEType , tag, NULL ));
252242
253- NSString *utiType = [( NSString *) UTTypeCreatePreferredIdentifierForTag ( kUTTagClassMIMEType , (CFStringRef) self .mimeType, NULL ) autorelease ] ;
254- NSString *pathExtension = [ (NSString *)UTTypeCopyPreferredTagWithClass ((CFStringRef)utiType, kUTTagClassFilenameExtension ) autorelease ] ;
243+ CFStringRef const tagClass = kUTTagClassFilenameExtension ;
244+ NSString *pathExtension = (NSString *)CFBridgingRelease ( UTTypeCopyPreferredTagWithClass ((__bridge CFStringRef)utiType, tagClass)) ;
255245
256246 if (pathExtension) {
257247
248+ NSFileManager * const fm = [NSFileManager defaultManager ];
249+
258250 NSString *fromPath = self.path ;
259251 NSString *toPath = [[self .path stringByDeletingPathExtension ] stringByAppendingPathExtension: pathExtension];
260252 NSError *error = nil ;
@@ -305,8 +297,9 @@ - (NSURLRequest *) connection:(NSURLConnection *)connection willSendRequest:(NSU
305297 if (!aRedirectResponse)
306298 return aRequest;
307299
308- NSMutableURLRequest *mutatedRequest = [[[ self underlyingRequest ] mutableCopy ] autorelease ];
300+ NSMutableURLRequest *mutatedRequest = [[self underlyingRequest ] mutableCopy ];
309301 mutatedRequest.URL = [aRequest URL ];
302+
310303 return mutatedRequest;
311304
312305}
@@ -382,25 +375,25 @@ - (NSString *) description {
382375
383376- (NSMutableArray *) appendedCompletionBlocks {
384377
385- if (appendedCompletionBlocks)
386- return appendedCompletionBlocks;
378+ if (!appendedCompletionBlocks) {
379+ appendedCompletionBlocks = [NSMutableArray array ];
380+ }
387381
388- appendedCompletionBlocks = [[NSMutableArray array ] retain ];
389382 return appendedCompletionBlocks;
390383
391384}
392385
393386- (void ) appendCompletionBlock : (void (^)(void ))aBlock {
394387
395- [self .appendedCompletionBlocks addObject: [[ aBlock copy ] autorelease ]];
388+ [self .appendedCompletionBlocks addObject: [aBlock copy ]];
396389
397390}
398391
399392- (void ) invokeCompletionBlocks {
400393
401394 @synchronized (self) {
402395
403- for (void (^aBlock)(void ) in [[ self .appendedCompletionBlocks copy ] autorelease ] )
396+ for (void (^aBlock)(void ) in self.appendedCompletionBlocks )
404397 aBlock ();
405398
406399 self.appendedCompletionBlocks = [NSMutableArray array ];
@@ -414,7 +407,7 @@ - (IRRemoteResourceDownloadOperation *) continuationOperationCancellingCurrentOp
414407 if (!self.path || !self.url )
415408 return nil ;
416409
417- __block IRRemoteResourceDownloadOperation *continuationOperation = [[[[ self class ] alloc ] init ] autorelease ];
410+ __block IRRemoteResourceDownloadOperation *continuationOperation = [[[self class ] alloc ] init ];
418411 continuationOperation.path = self.path ;
419412 continuationOperation.url = self.url ;
420413 continuationOperation.preferredByteOffset = self.processedBytes ;
0 commit comments