title: Download Image for CoreData
body: <p>Im trying to create multiple CoreData Objects containing a name and an image but the object doesn't save. If I put the saving command outside the URLSession then the picture doesn't load. is there another option?</p>
  
  <p>let context = self.appDelegate.persistentContainer.viewContext</p>
  
  <pre><code>let newDeputy = Deputy(context: context)
  newDeputy.name = "someName"
  
  URLSession.shared.dataTask(with: URL(string: deputy.personal.picture!.url)!) { data, response, error in
      if data != nil {
          if let imageData = UIImage(data: data!)!.jpegData(compressionQuality: 0.3) {
              newDeputy.picture = imageData
          }
      }
  
      do {
          try newDeputy.managedObjectContext?.save()
          print("SAVED \(newDeputy.name)")
      } catch let error as NSError {
          print("Couldnt save Deputy (\(newDeputy.name ?? "")) - \(error.localizedDescription)")
      }
  }.resume()
  </code></pre>