Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.91 KB | None | 0 0
  1.     func start() {
  2.         if let t = task {
  3.             if t.state == .suspended {
  4.                 t.resume()
  5.             }
  6.         }
  7.         else {
  8.             if self.photo.servingUrlImage == nil {
  9.                 PhotoUtil.util.arrPhotosBeingDownloaded.removeDownload(self)
  10.                 PhotoUtil.util.continueDownloading(self.photo)
  11.             }
  12.            
  13.             if let url = URL(string: self.photo.servingUrlImage) {
  14.                 let session = URLSession(configuration: URLSessionConfiguration.default)
  15.                
  16.                
  17.                 let request = Foundation.URLRequest(url: url)
  18.                 task = session.dataTask(with: request, completionHandler: { data, response, error in
  19.                     do {
  20.                         if error == nil {
  21.                             if let image = UIImage(data: data!) {
  22.                                 self.callback?(image)
  23.                                 serialQueue.sync {
  24.                                     PhotoUtil.util.arrPhotosBeingDownloaded.removeDownload(self)
  25.                                     PhotoUtil.util.continueDownloading(self.photo)
  26.                                 }
  27.                                 PhotosUtil.savePic(self.photo.id, image: image)
  28.                                 self.photo.downloadedStatus = .downloaded
  29.                                
  30.                                 DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
  31.                                     session.finishTasksAndInvalidate()
  32.                                 })
  33.                             } else {
  34.                                 print("error getting image")
  35.                             }
  36.                         } else {
  37.                             print(error?.localizedDescription)
  38.                         }
  39.                     }
  40.                 })
  41.                 task?.resume()
  42.             }
  43.         }
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement