urlsession reloaded

39
NS URLSession Reloaded Yokohama iOS Developers Meeting Nov. 2016 Kaz Yoshikawa [email protected]

Upload: kaz-yoshikawa

Post on 13-Feb-2017

63 views

Category:

Engineering


0 download

TRANSCRIPT

NSURLSessionReloaded

Yokohama iOS Developers Meeting Nov. 2016

Kaz Yoshikawa [email protected]

URLSession Reloaded

About me

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.

URLSession Reloaded

What do you use for downloading?

URLSession Reloaded

Downloading Options

• CFNetwork

• AFNetworking

• Alamofire

• NSOperation or GCD + NSData(contentsOf:) *

• NSURLSession + NSURLSessionTask

* or equivalent

URLSession Reloaded

Alamofire vs URLSession

left Intentionally blank

URLSession Reloaded

URLSessionTask

URLSession Reloaded

URLSessionTask

URLSessionDataTask

URLSessionDownloadTask

URLSessionUploadTask

URLSession Reloaded

URLSessionDataTask

left Intentionally blank

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

URLSession Reloaded

Download Task Code

URLSession Reloaded

Case Study News App

URLSession Reloaded

News Type of App

catalog

features

sports

economy

article

article

current affairs

photo

photo

download

adsads

URLSession Reloaded

List of Contents (< 1MB)

catalog

features

sports

economy

article

article

current affairs

photo

photo

*

* total

adsads

URLSession Reloaded

Data Task

catalog

features

sports

economy

article

article

current affairs

photo

photo

adsads

URLSession Reloaded

Contents (>10MB)

catalog

features

sports

economy

article

article

current affairs

photo

photo

* total

*

adsads

URLSession Reloaded

Download Task

catalog

features

sports

economy

article

article

current affairs

photo

photo

adsads

URLSession Reloaded

Download Task

catalog

features

sports

economy

article

article

current affairs

photo

photo

adsads

URLSession Reloaded

How to identify the client of downloaded binary?

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

URLSession Reloaded

URLSessionTask

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

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

😔

URLSession Reloaded

You underestimate the power of String..

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

URLSession Reloaded

Extracting JSON from taskDescription

URLSession Reloaded

UIApplicationDelegate

URLSession Reloaded

ZDownloader

https://gist.github.com/codelynx/806913e8b4122d5ea0997fad386b97a0

URLSession Reloaded

ZDownloader architecture

ZDownloader ZDownloadable

ZDownloaderDelegate

request

URLSession

requestdelegate

find

find

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

URLSession Reloaded

ZDownloadDelegate

• Knows how to find or create ZDownloadable from JSON

URLSession Reloaded

ZDownloadable

• Represent a downloadable object

• NSDictionary to represent that downloadable object uniquely

• Will get called when download is completed or error

URLSession Reloaded

Sample Project Image Downloader

URLSession Reloaded

ZDownloader architecture

ZDownloader ZDownloadable

ZDownloaderDelegate

request

URLSession

requestdelegate

find

find

URLSession Reloaded

ZDownloader architecture

ZDownloader ZDownloadable

ZDownloaderDelegate

request

URLSession

requestdelegate

find

find

URLSession Reloaded

Sample Project

URLSession Reloaded

ZDownloaderSample

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

URLSession Reloaded

Thanks