title: Downloading images serially in a serial queue very slow
body: <p><strong>Requirement</strong> <strong>-</strong> I have a requirement in which I am receiving a JSON dictionary from which I am retrieving an array of images and content text. Then I have to display all the images with corresponding contents in a collection view. </p>
  
  <p><strong><em>Update -</em></strong> Above all I need to calculate the cell size based on image size scaled to the a constant width for which I fell that(may not be correct) I need all images to be downloaded completely then reload collection view</p>
  
  <p><strong>Problem -</strong> But the problem is that when I download the images in background thread and populate in separate arrays.Then the image cannot be added in the same order as they were in the JSON Dictionary since I am downloading them in a concurrent queue. </p>
  
  <p><strong>My Solution -</strong> So I thought of downloading them by putting everything in a serial queue which has made my retrieving data very slow. <strong><em>What can be an efficient alternative for this?</em></strong></p>
  
  <p><strong>Code -</strong> </p>
  
  <pre><code>let serialQueue = dispatch_queue_create("my serial queue", nil)
  
              dispatch_async(serialQueue, {
  
                  print("This is first Method")
  
                  for var i=0;i&lt;self.resultArr.count;i++//resultArr is my array of data's in the jsonDic
               {
  
  
  
                      sleep(2)
  
                      print(self.resultArr[i].valueForKey("profile_pic")! as! String)
                      if self.resultArr[i].valueForKey("profile_pic")! as! String != "Null" &amp;&amp; self.resultArr[i].valueForKey("profile_pic")! as! String != "null" &amp;&amp; self.resultArr[i].valueForKey("profile_pic")! as! String != "NULL" &amp;&amp; self.resultArr[i].valueForKey("profile_pic")! as! String != ""
                      {
                          let imageUrl = UrlClass.imageUrlWithoutExtension + String(self.resultArr[i].valueForKey("profile_pic")!)
                          print(imageUrl)
                          let url = NSURL(string: imageUrl)
                          let imageData = NSData(contentsOfURL: url!)
  
                          self.contentlabelArr.insertObject(String(self.resultArr[i].valueForKey("content")!), atIndex: i)
  
                          if imageData != nil &amp;&amp; imageData?.length &gt; 0
                          {
                              print("this is \(i) image")
                              print(UIImage(data: imageData!))
  
                              self.imageArr.insertObject(UIImage(data: imageData!)!, atIndex: i)
                          }
                          else
                          {
                              print("\(i) image has nill")
                              self.imageArr.insertObject(UIImage(named: "logo.png")!, atIndex: i)
                          }
  
                      }
                      else
                      {
                          print("\(i) image has nill")
                          self.contentlabelArr.insertObject(String(self.resultArr[i].valueForKey("content")!), atIndex: i)
                          self.imageArr.insertObject(UIImage(named: "logo.png")!, atIndex: i)
                      }
  
                      print("\(i) times 5 is \(i * 5)")
  
                      if self.imageArr.count==self.resultArr.count
                      {
                           print(self.resultArr.count)
                           print(self.imageArr.count)
                          dispatch_async(dispatch_get_main_queue(),
                              {
                                  print(self.resultArr.count)
                                  print(self.imageArr.count)
                                  print(self.imageArr)
                                  print(self.contentlabelArr)
                                  self.collectionView?.reloadData()
                          })
                      }
  </code></pre>