urlsession reloaded
TRANSCRIPT
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
Downloading Options
• CFNetwork
• AFNetworking
• Alamofire
• NSOperation or GCD + NSData(contentsOf:) *
• NSURLSession + NSURLSessionTask
* or equivalent
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
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
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
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
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
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
ZDownloadable
• Represent a downloadable object
• NSDictionary to represent that downloadable object uniquely
• Will get called when download is completed or error
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
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