title: SDWebImage's sd_setImageWithURL updates the wrong cell with Image on scrolling!! Is that a expected behaviour?
body: <p><strong>OverView</strong></p>
  
  <p>I am downloading the images from web server using SDWebImage in my collectionView cell. </p>
  
  <pre><code>if floor.hasTitleImage != nil {
              self.floorImageView.sd_setImageWithURL(NSURL(string:(floor.hasTitleImage?.imageURL)!), placeholderImage: UIImage(named: "Placeholder"), completed: { (imageDownloaded, error, cacheType, url) in
                  if imageDownloaded.size.width &gt; 300 {
                      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
                          let resizedImage = imageDownloaded.resizePreservingAspectRatio(toSize: CGSize(width: 300, height: 300))
                          SDImageCache.sharedImageCache().removeImageForKey(NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString, fromDisk: true, withCompletion: {
                              SDImageCache.sharedImageCache().storeImage(resizedImage, forKey: NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString, toDisk: true)
                          })
                      })
                  }
              })
          }
          else{
              self.floorImageView.image = UIImage(named: "Placeholder")
          }
  </code></pre>
  
  <p>Now what this code is doing is downloading the image using the url stored in core data element called floor. Now the images downloaded are way bigger then my ImageView so am resizing it to somewhere around 300px.</p>
  
  <p>I know that SDWebImage does intense caching by default and uses url's absolute string as key for its cache. So Once resized! I replace the original image with my resized image.</p>
  
  <p><strong>Issue</strong></p>
  
  <p>Everything worked fine as expected! till I tested it with low internet speed which caused the image download to be delayed.</p>
  
  <p>If the image downloading takes time, till then if I scroll the tableView and the cell gets reused, once the image downloaded SDWebImage loads the imageView of the cell but ends up resulting the wrong cell with wrong image.</p>
  
  <p>Though this happens only once as for next time onwards image will be loaded from cache everything works fine. But Is this the expected behaviour or am I doing something wrong???</p>
  
  <p><strong>Solutions I could think of :</strong></p>
  
  <ol>
  <li><p>As sd_setImageWithURL by default sets the value of imageView with downloaded image rather than downloading the image using sd_setImageWithURL, if I can download it using,</p>
  
  <pre><code>if floor.hasTitleImage != nil {
      if let downloadedImage = SDImageCache.sharedImageCache().imageFromMemoryCacheForKey(NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString){
          self.floorImageView.image = downloadedImage
      }
      else if let diskImage = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString){
          self.floorImageView.image = diskImage
      }
      else{
          let downloadManager = SDWebImageManager.sharedManager();
          downloadManager.downloadImageWithURL(NSURL(string:(floor.hasTitleImage?.imageURL)!), options: [], progress: nil, completed: { (downloadedImage, error, cacheType, finished, url) in
              if downloadedImage.size.width &gt; 300 {
                  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
                      let resizedImage = downloadedImage.resizePreservingAspectRatio(toSize: CGSize(width: 300, height: 300))
                      SDImageCache.sharedImageCache().removeImageForKey(NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString, fromDisk: true, withCompletion: {
                          SDImageCache.sharedImageCache().storeImage(resizedImage, forKey: NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString, toDisk: true)
                      })
                      if floor.hasTitleImage?.imageURL != nil &amp;&amp; floor.hasTitleImage?.imageURL! == url{
                          dispatch_async(dispatch_get_main_queue(), {
                              self.floorImageView.image = SDImageCache.sharedImageCache().imageFromMemoryCacheForKey(NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString)
                              self.reloadInputViews()
                          })
                      }
                  })
              }
          })
      }
  }
  else{
      self.floorImageView.image = UIImage(named: "Placeholder")
  }
  </code></pre></li>
  </ol>
  
  <p>Though it works it will load the image only when I scroll the tableView :(</p>
  
  <p><strong>Questions :</strong></p>
  
  <ol>
  <li><p>Is what sd_setImageWithURL doing is expected behaviour ?? I mean every single one of the app using SDWebImage with tableView or CollectionView must have faced this correct ?? So if no body faced it I must be doing something wrong :( Am I doing anything wrong ?</p></li>
  <li><p>Is what am doing using SDWebImageManager.sharedManager is correct ??? Is that long code required? Checking memory cache then disk cache and finally making call to download image isn't it too much :o</p></li>
  <li><p>Why is that my second approach not loading the image once downloaded and expects the collection view to scroll and reload the cell to display the image ??</p></li>
  </ol>
  
  <p><strong>EDIT</strong></p>
  
  <p>I solved the 3rd question :) Sorry my mistake. I was comparing <code>if floor.hasTitleImage?.imageURL != nil &amp;&amp; floor.hasTitleImage?.imageURL! == url{</code> which is wrong because url above is <code>NSURL</code> where as floor.hasTitleImage?.imageURL! is <code>NSString</code>.</p>
  
  <p>So Updated solution :</p>
  
  <pre><code>if floor.hasTitleImage?.imageURL != nil &amp;&amp; floor.hasTitleImage!.imageURL! == url.absoluteString{
                                  dispatch_async(dispatch_get_main_queue(), {
                                      self.floorImageView.image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString)
                                      self.setNeedsLayout()
                                  })
                              }
  </code></pre>
  
  <p>Though my second approach downloads the image now and updates the cell properly but now image downloading is very slow compared to sd_setImageWithURL.</p>
  
  <p>Is there any way I can use the method1 sd_setImageWithURL and get the image updated properly?</p>
  
  <p>Please help me!! what am doing wrong !! Thanks in advance :)</p>