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

Gwendolyn Weston "While Your App Was Sleeping"

Avatar for Realm Realm
October 08, 2015
55k

Gwendolyn Weston "While Your App Was Sleeping"

Avatar for Realm

Realm

October 08, 2015
Tweet

Transcript

  1. A Teakettle • That requires you to stand near it

    • And watch it in order for it to boil water
  2. Overview • How to download in the foreground • How

    to make that request background compatible • How to navigate common pitfalls
  3. Downloading in Foreground let urlstring = "https://remoteteakettle.com/boiledwater.pdf" let filepath =

    "Documents/local_teakettle" if let url = NSURL(string: urlstring) { let task = NSURLSession.sharedSession().downloadTaskWithURL(url, completionHandler: { (location:NSURL?, response:NSURLResponse?, error:NSError?) in if let loc = location, path = loc.path { try! NSFileManager.defaultManager().moveItemAtPath(path, toPath:filepath) } }) task.resume() }
  4. Downloading in Foreground NSURLSession: vends and manages requests let urlstring

    = "https://remoteteakettle.com/boiledwater.pdf" let filepath = "Documents/local_teakettle" if let url = NSURL(string: urlstring) { let task = NSURLSession.sharedSession().downloadTaskWithURL(url, completionHandler: { (location:NSURL?, response:NSURLResponse?, error:NSError?) in if let loc = location, path = loc.path { try! NSFileManager.defaultManager().moveItemAtPath(path, toPath:filepath) } }) task.resume() }
  5. Downloading in Foreground sharedSession: singleton session with default settings let

    urlstring = "https://remoteteakettle.com/boiledwater.pdf" let filepath = "Documents/local_teakettle" if let url = NSURL(string: urlstring) { let task = NSURLSession.sharedSession().downloadTaskWithURL(url, completionHandler: { (location:NSURL?, response:NSURLResponse?, error:NSError?) in if let loc = location, path = loc.path { try! NSFileManager.defaultManager().moveItemAtPath(path, toPath:filepath) } }) task.resume() }
  6. Downloading in Foreground NSURLSessionDownloadTask: the request let urlstring = "https://remoteteakettle.com/boiledwater.pdf"

    let filepath = "Documents/local_teakettle" if let url = NSURL(string: urlstring) { let task = NSURLSession.sharedSession().downloadTaskWithURL(url, completionHandler: { (location:NSURL?, response:NSURLResponse?, error:NSError?) in if let loc = location, path = loc.path { try! NSFileManager.defaultManager().moveItemAtPath(path, toPath:filepath) } }) task.resume() }
  7. Downloading in Foreground let urlstring = "https://remoteteakettle.com/boiledwater.pdf" let filepath =

    "Documents/local_teakettle" if let url = NSURL(string: urlstring) { let task = NSURLSession.sharedSession().downloadTaskWithURL(url, completionHandler: { (location:NSURL?, response:NSURLResponse?, error:NSError?) in if let loc = location, path = loc.path { try! NSFileManager.defaultManager().moveItemAtPath(path, toPath:filepath) } }) task.resume() }
  8. Downloading in Foreground let urlstring = "https://remoteteakettle.com/boiledwater.pdf" let filepath =

    "Documents/local_teakettle" if let url = NSURL(string: urlstring) { let task = NSURLSession.sharedSession().downloadTaskWithURL(url, completionHandler: { (location:NSURL?, response:NSURLResponse?, error:NSError?) in if let loc = location, path = loc.path { try! NSFileManager.defaultManager().moveItemAtPath(path, toPath:filepath) } }) task.resume() }
  9. Life of a background request • Requests start in foreground

    • App dies • All requests in session finish in background • App starts back up in background
  10. Recreating Tasks The systems knows which tasks to recreate by

    the session configuration identifier alone.
  11. Life of a background request • Request starts in foreground

    • App dies • Request finishes in background • App starts back up in background • App hits NSURLSessionDownloadDelegate methods
  12. Recipe • Create custom session with background configuration • Move

    code from completion handlers to delegate methods
  13. Recipe • Create custom session with background configuration • Move

    code from completion handlers to delegate methods • Persist any information needed for request callback
  14. let urlstring = "https://remoteteakettle.com/boiledwater.pdf" let filepath = "Documents/local_teakettle" if let

    url = NSURL(string: urlstring) { let task = NSURLSession.sharedSession().downloadTaskWithURL(url, completionHandler: { (location:NSURL, response:NSURLResponse?, error:NSError?) in if let path = location.path { try! NSFileManager.defaultManager().moveItemAtPath(path, toPath:filepath) } }) task.resume() } Foreground Background 1. background config 2. callback 3. persist request