title: Uploading Multiple Files with AFNetworking - UIViewController not Deallocating
body: <p>I have to upload multiple files, track their progress &amp; subscribe to completion &amp; failure blocks in order to show relevant message at the end of operation.</p>
  
  <p>I wrote my own AFHTTPClient wrapper and created following method.</p>
  
  <pre><code>- (void) uploadFiles:(NSArray*)files
                  path:(NSString*)path
            parameters:(NSDictionary*)parameters
         progressBlock:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))block
               success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure
  {
      NSMutableURLRequest *request =
      [self multipartFormRequestWithMethod:@"POST"
                                      path:path
                                parameters:parameters
                 constructingBodyWithBlock:
       ^(id &lt;AFMultipartFormData&gt;formData) {
  
           for (CRLMultiPartFile *file in files) {
               NSAssert(file.name, @"Name cannot be nil");
               NSAssert(file.file, @"Nothing found to upload");
               NSAssert(file.filename, @"FileName cannot be nil");
               NSAssert(file.mimeType, @"Must set Mime-Type for %@", file.filename);
               [formData appendPartWithFileData:file.file name:file.name fileName:file.filename mimeType:file.typeString];
           }
      }];
  
      AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
      [operation setUploadProgressBlock:block];
      [operation setCompletionBlockWithSuccess:success failure:failure];
      [self enqueueHTTPRequestOperation:operation];
  }
  </code></pre>
  
  <p>The view controller that calls this method does not get deallocated and therefore all the images contained are retained in memory as well thus resulting in memory leaks and eventually memory warning.</p>
  
  <p>Doing the profiling reveals that at the end of the whole operation, the view controller has a <code>refCount</code> of 1.</p>
  
  <p>When I comment out the call to uploading the files, all works fine.</p>
  
  <p>Here is the code in the controller. It uses the progress block to update elements on the UI.</p>
  
  <pre><code>NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
  ContactModel *model = (ContactModel*)[self.contacts lastObject];
  [params setObject:model.phone forKey:@"receiver"];
  
  __block typeof(self) sSelf = self;
  
  [[JMClient sharedClient] uploadFiles:files
                                  path:@"picture_share/"
                            parameters:params
                         progressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
  
                             CGFloat progPercent = ceilf(((CGFloat)totalBytesWritten / (CGFloat)totalBytesExpectedToWrite) * 100);
  
                             CGFloat widthToCut = (progPercent * sSelf.progWidth) / 100;
  
                             CGRect frame = sSelf.progresViewBG.frame;
                             frame.size.width = (sSelf.progWidth  - widthToCut);
                             frame.origin.x   = (sSelf.progOrigin + widthToCut);
                             sSelf.progresViewBG.frame = frame;
  
                             sSelf.progLabel.text = [NSString stringWithFormat:@"%i%%", (int)progPercent];
  
                             frame = sSelf.progTipView.frame;
                             frame.origin.x = (sSelf.progresViewBG.frame.origin.x - frame.size.width/2);
                             sSelf.progTipView.frame = frame;
  
                             frame = sSelf.progLabel.frame;
                             frame.origin.x = (sSelf.progresViewBG.frame.origin.x - frame.size.width/2);
                             sSelf.progLabel.frame = frame;
  
                         } success:^(AFHTTPRequestOperation *success, id reponse) {
  
                             CGRect frame = sSelf.progresViewBG.frame;
                             frame.size.width = 0;
                             frame.origin.x = sSelf.progOrigin;
                             sSelf.progresViewBG.frame = frame;
                             [sSelf.cancelButton setImage:[UIImage imageNamed:@"trnsfr_prgss_complt.png"] forState:UIControlStateNormal];
  
                             [sSelf performSelector:@selector(hideAwayProgressBars) withObject:nil afterDelay:3];
  
                         } failure:^(AFHTTPRequestOperation *failure, NSError *error) {
  
                             [Mediator showMessage:TGLocalizedString(kMessageKeyForUploadingFailed)];
                             [sSelf performSelector:@selector(hideAwayProgressBars) withObject:nil afterDelay:3];
  
                         }];
  
  self.operation = [[self.client.sharedClient.operationQueue operations] lastObject];
  
  - (void) hideAwayProgressBars
  {
      [[NSNotificationCenter defaultCenter] postNotificationName:kNotifcationKeyForPhotoUploadComplete object:nil];
  }
  </code></pre>
  
  <p>The notification is received by the parent controller, that removes this controller's view from superview and sets it to nil.</p>
  
  <p>P.S. CRLMultiPartFile is a custom class to hold attributes of the files to be uploaded</p>