URLSession Reloaded

79874cedccd1cf5baa8cb264b5091ee6?s=47 codelynx
November 26, 2016

URLSession Reloaded

Tips and tricks how to use URLSessionDownloadTask and it's utility class ZDownloader and it's sample project/code Image Downloader.

79874cedccd1cf5baa8cb264b5091ee6?s=128

codelynx

November 26, 2016
Tweet

Transcript

  1. NSURLSession
 Reloaded Yokohama iOS Developers Meeting Nov. 2016 Kaz Yoshikawa

    kyoshikawa@electricwoods.com
  2. URLSession Reloaded About me

  3. Kaz Yoshikawa • Electricwoods LLC ୅ද / Digital Lynx Systems

    Inc. ෭୅ද • e-mail: kyoshikawa@electricwoods.com • twitter: @codelynx1 • Working History • Adobe Systems (Tokyo) • Lionbridge (Tokyo) • Quark (Tokyo / Denver) • Hummingbird Communications (Mt. View, USA) • Fact International (Vancouver, Canada) • Perle Systems (Toronto, Canada), etc.
  4. URLSession Reloaded What do you use for downloading?

  5. URLSession Reloaded Downloading Options • CFNetwork • AFNetworking • Alamofire

    • NSOperation or GCD + NSData(contentsOf:) * • NSURLSession + NSURLSessionTask * or equivalent
  6. URLSession Reloaded Alamofire vs URLSession left Intentionally blank

  7. URLSession Reloaded URLSessionTask

  8. URLSession Reloaded URLSessionTask URLSessionDataTask URLSessionDownloadTask URLSessionUploadTask

  9. URLSession Reloaded URLSessionDataTask left Intentionally blank

  10. URLSession Reloaded URLSessionDownloadTask • Downloads in foreground or background •

    download continues while app is killed or sleeping • Callback closure is useless — if app is killed by system • Use delegate method to catch up what have been downloaded
  11. URLSession Reloaded Download Task Code DMBTT.Z$POUFOUT/40CKFDU 63-4FTTJPO%PXOMPBE%FMFHBUF\ GVODVSM4FTTJPO @TFTTJPO63-4FTTJPO EPXOMPBE5BTL63-4FTTJPO%PXOMPBE5BTL

    
 EJE'JOJTI%PXOMPBEJOH5PMPDBUJPO63- \ TBWFPSQSPDFTTEPXOMPBEFEGJMF ^    GVODVSM4FTTJPO @TFTTJPO63-4FTTJPO UBTL63-4FTTJPO5BTL 
 EJE$PNQMFUF8JUI&SSPSFSSPS&SSPS \ FSSPSIBOEMJOH  ^ ^ MFUNZ$POUFOUT.Z$POUFOUT  MFUDPOGJHVSBUJPO63-4FTTJPO$POGJHVSBUJPOCBDLHSPVOE 
 XJUI*EFOUJGJFSNZDPOUFOUT  DPOGJHVSBUJPOBMMPXT$FMMVMBS"DDFTTGBMTF MFUTFTTJPO63-4FTTJPO DPOGJHVSBUJPODPOGJHVSBUJPO  EFMFHBUFNZ$POUFOUT EFMFHBUF2VFVFOJM  MFUUBTLTFTTJPOEPXOMPBE5BTL XJUI63- TUSJOHIUUQBQQMFDPN   UBTLSFTVNF
  12. URLSession Reloaded Case Study News App

  13. URLSession Reloaded News Type of App catalog features sports economy

    article article current affairs photo photo download ads ads
  14. URLSession Reloaded List of Contents (< 1MB) catalog features sports

    economy article article current affairs photo photo * * total ads ads
  15. URLSession Reloaded Data Task catalog features sports economy article article

    current affairs photo photo ads ads
  16. URLSession Reloaded Contents (>10MB) catalog features sports economy article article

    current affairs photo photo * total * ads ads
  17. URLSession Reloaded Download Task catalog features sports economy article article

    current affairs photo photo ads ads
  18. URLSession Reloaded Download Task catalog features sports economy article article

    current affairs photo photo ads ads
  19. URLSession Reloaded How to identify the client of downloaded binary?

  20. URLSession Reloaded URLSessionDownloadDelegate func urlSession(_ session: URLSession, 
 downloadTask: URLSessionDownloadTask,

    
 didFinishDownloadingTo location: URL) How do I know which article or photo or ad is about this download !? Check the URL ☹ No way !!
  21. URLSession Reloaded URLSessionTask

  22. URLSession Reloaded taskIdentifier let url = URL(string: "https://www.apple.com")! let task

    = session.dataTask(with: url) { (data, response, error) in if let data = data { print(data as NSData) } } task.resume() task.taskIdentifier // task-id Integer • Needs keep this ID associate with article-id or other id • May be similar to use URL to identify the download owner • If you have other sessions, the ID may collide
  23. URLSession Reloaded taskDescription • What you want to save here?

    • article-id?, path to save file?, managed object ID? • But this property is just a string let url = URL(string: "https://www.apple.com")! let task = session.dataTask(with: url) { (data, response, error) in if let data = data { print(data as NSData) } } task.taskDescription = "Some useful information"
 task.resume() String
  24. URLSession Reloaded You underestimate the power of String..

  25. URLSession Reloaded Power of String JSON base64 Basically, you can

    put pretty much anything, but don't go too far… These are Strings as well
  26. URLSession Reloaded Extracting JSON from taskDescription VODVSM4FTTJPO @TFTTJPO63-4FTTJPO EPXOMPBE5BTL63-4FTTJPO%PXOMPBE5BTL 

    EJE'JOJTI%PXOMPBEJOH5PMPDBUJPO63- \ JGMFUEFTDSJQUJPOEPXOMPBE5BTLUBTL%FTDSJQUJPO  MFUEBUBEFTDSJQUJPOEBUB VTJOHVUG  MFUKTPOUSZ+40/4FSJBMJ[BUJPOKTPO0CKFDU XJUIEBUB PQUJPOT<> 
 BT /4%JDUJPOBSZ\ JGMFUUZQFKTPO<UZQF>BT 4USJOH\ TXJUDIUZQF\ DBTFBSUJDMF JGMFUBSUJDMF*%KTPO<BJE>BT 4USJOH  MFUQBUI5P4BWFKTPO<QBUI>BT 4USJOH\ MFUQBUI QBUI5P4BWFBT/44USJOH FYQBOEJOH5JMEF*O1BUI NPWFGJMFGSPNMPDBUJPOUPUIJTQBUI ^ DBTFBETǘ DBTFJNBHFǘ EFGBVMUCSFBL ^ ^ ^ ^
  27. URLSession Reloaded UIApplicationDelegate DMBTT"QQ%FMFHBUF6*3FTQPOEFS 6*"QQMJDBUJPO%FMFHBUF\ 
 GVODBQQMJDBUJPO @BQQMJDBUJPO6*"QQMJDBUJPO 
 IBOEMF&WFOUT'PS#BDLHSPVOE63-4FTTJPOJEFOUJGJFS4USJOH

    
 DPNQMFUJPO)BOEMFS!FTDBQJOH 7PJE \ 
 $BMMFEXIFOUIFBQQMJDBUJP DPNQMFUJPO)BOEMFS  ^ 
 ^
  28. URLSession Reloaded ZDownloader https://gist.github.com/codelynx/806913e8b4122d5ea0997fad386b97a0

  29. URLSession Reloaded ZDownloader architecture ZDownloader ZDownloadable ZDownloaderDelegate request URLSession request

    delegate find find
  30. URLSession Reloaded ZDownloader • Should be setup at launch time

    • Schedule downloading • A ZDownloader for a session • delegate of URLSession • ask ZDownloaderDelegate to fetch / construct ZDownloadable • decode JSON and pass downloaded binary to ZDownloadable
  31. URLSession Reloaded ZDownloadDelegate • Knows how to find or create

    ZDownloadable from JSON
  32. URLSession Reloaded ZDownloadable • Represent a downloadable object • NSDictionary

    to represent that downloadable object uniquely • Will get called when download is completed or error
  33. URLSession Reloaded Sample Project Image Downloader

  34. URLSession Reloaded ZDownloader architecture ZDownloader ZDownloadable ZDownloaderDelegate request URLSession request

    delegate find find
  35. URLSession Reloaded ZDownloader architecture ZDownloader ZDownloadable ZDownloaderDelegate request URLSession request

    delegate find find
  36. URLSession Reloaded Sample Project

  37. URLSession Reloaded ZDownloaderSample

  38. URLSession Reloaded ZDownloaderSample • Pick 256 images from 5000 URLs

    randomly • Download images in background • Show downloaded images in Collection View • Tap "reload" to re-pick 256 images and download
  39. URLSession Reloaded Thanks