Slide 1

Slide 1 text

NSURLSession
 Reloaded Yokohama iOS Developers Meeting Nov. 2016 Kaz Yoshikawa [email protected]

Slide 2

Slide 2 text

URLSession Reloaded About me

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

URLSession Reloaded What do you use for downloading?

Slide 5

Slide 5 text

URLSession Reloaded Downloading Options • CFNetwork • AFNetworking • Alamofire • NSOperation or GCD + NSData(contentsOf:) * • NSURLSession + NSURLSessionTask * or equivalent

Slide 6

Slide 6 text

URLSession Reloaded Alamofire vs URLSession left Intentionally blank

Slide 7

Slide 7 text

URLSession Reloaded URLSessionTask

Slide 8

Slide 8 text

URLSession Reloaded URLSessionTask URLSessionDataTask URLSessionDownloadTask URLSessionUploadTask

Slide 9

Slide 9 text

URLSession Reloaded URLSessionDataTask left Intentionally blank

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

URLSession Reloaded Case Study News App

Slide 13

Slide 13 text

URLSession Reloaded News Type of App catalog features sports economy article article current affairs photo photo download ads ads

Slide 14

Slide 14 text

URLSession Reloaded List of Contents (< 1MB) catalog features sports economy article article current affairs photo photo * * total ads ads

Slide 15

Slide 15 text

URLSession Reloaded Data Task catalog features sports economy article article current affairs photo photo ads ads

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

URLSession Reloaded How to identify the client of downloaded binary?

Slide 20

Slide 20 text

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 !!

Slide 21

Slide 21 text

URLSession Reloaded URLSessionTask

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

URLSession Reloaded You underestimate the power of String..

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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 ^ ^ ^ ^

Slide 27

Slide 27 text

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  ^ 
 ^

Slide 28

Slide 28 text

URLSession Reloaded ZDownloader https://gist.github.com/codelynx/806913e8b4122d5ea0997fad386b97a0

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

URLSession Reloaded ZDownloadDelegate • Knows how to find or create ZDownloadable from JSON

Slide 32

Slide 32 text

URLSession Reloaded ZDownloadable • Represent a downloadable object • NSDictionary to represent that downloadable object uniquely • Will get called when download is completed or error

Slide 33

Slide 33 text

URLSession Reloaded Sample Project Image Downloader

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

URLSession Reloaded Sample Project

Slide 37

Slide 37 text

URLSession Reloaded ZDownloaderSample

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

URLSession Reloaded Thanks