Upgrade to Pro — share decks privately, control downloads, hide ads and more …

URLSession Reloaded

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.

codelynx

November 26, 2016
Tweet

More Decks by codelynx

Other Decks in Programming

Transcript

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

    Inc. ෭୅ද • e-mail: [email protected] • 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.
  2. URLSession Reloaded Downloading Options • CFNetwork • AFNetworking • Alamofire

    • NSOperation or GCD + NSData(contentsOf:) * • NSURLSession + NSURLSessionTask * or equivalent
  3. 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
  4. 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
  5. URLSession Reloaded News Type of App catalog features sports economy

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

    economy article article current affairs photo photo * * total ads ads
  7. 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 !!
  8. 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
  9. 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
  10. 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
  11. 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 ^ ^ ^ ^
  12. 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
  13. URLSession Reloaded ZDownloadable • Represent a downloadable object • NSDictionary

    to represent that downloadable object uniquely • Will get called when download is completed or error
  14. 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