title: NSURLConnection finished with error - code -1002 when retrieving and displaying images
body: <p>For anyone having this problem, who is not helped by this post, you may find the following use-full: <a href="https://stackoverflow.com/questions/48991149/nsurlconnection-finished-with-error-code-1002-fix-does-not-work">Here</a>.</p>
  <p>Here is a copy of the error:</p>
  <blockquote>
  <p>2018-08-29 18:39:59.458950-0400 proj[5319:3378682] NSURLConnection finished with error - code -1002</p>
  </blockquote>
  <p>I have looked through these answer and not found anything to work. I believe its because the problem is happening for another reason for those. I am having the problem when fetching images from a database and trying to display them in on a image view.</p>
  <p>Full code bellow:</p>
  <pre><code>import UIKit
  import FirebaseStorage
  import FirebaseDatabase
  import FirebaseAuth
  import Firebase
  
  class PhaseOneViewController: UIViewController {
      
      @IBOutlet weak var p1ImageView: UIImageView!
  
      @IBAction func loadImages(_ sender: Any) {
          //p1ImageView.image = nil
          NKPlaceholderImage(image: UIImage(named: &quot;placeholder&quot;), imageView: p1ImageView, imgUrl: &quot;\(Storage.storage().reference().child((Auth.auth().currentUser?.uid)!).child(&quot;post\(takePicViewController().finalPost + PhotoArray.sharedInstance.numberPost)&quot;).child(ImageUploadManager().imageName))&quot;) { (image) in }
  
      }
  
      func NKPlaceholderImage(image:UIImage?, imageView:UIImageView?,imgUrl:String,compate:@escaping (UIImage?) -&gt; Void) {
          
          if image != nil &amp;&amp; imageView != nil {
              imageView!.image = image!
          }
          
          var urlcatch = imgUrl.replacingOccurrences(of: &quot;/&quot;, with: &quot;#&quot;)
          let documentpath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
          urlcatch = documentpath + &quot;/&quot; + &quot;\(urlcatch)&quot;
          
          let image = UIImage(contentsOfFile:urlcatch)
          if image != nil &amp;&amp; imageView != nil
          {
              imageView!.image = image!
              compate(image)
              
          }else{
              
              if let url = URL(string: imgUrl){
                  
                  DispatchQueue.global(qos: .background).async {
                      () -&gt; Void in
                      let imgdata = NSData(contentsOf: url)
                      DispatchQueue.main.async {
                          () -&gt; Void in
                          imgdata?.write(toFile: urlcatch, atomically: true)
                          let image = UIImage(contentsOfFile:urlcatch)
                          compate(image)
                          if image != nil  {
                              if imageView != nil  {
                                  imageView!.image = image!
                              }
                          }
                      }
                  }
              }
          }
      }
  
  }
  </code></pre>
  <p>I have also asked These questions <a href="https://stackoverflow.com/questions/52069139/images-not-displaying-in-imageview#comment91112438_52069218">Link</a>, <a href="https://stackoverflow.com/questions/52029400/firebase-saving-last-image-in-array-to-storage-twice">Link</a>, which may be of help to someone in my situation or to solve this problem.</p>
  <p>Thanks!</p>