Gwendolyn Weston "While Your App Was Sleeping"

1fa9cb8c7997c8c4d3d251fb5e41f749?s=47 Realm
October 08, 2015
48k

Gwendolyn Weston "While Your App Was Sleeping"

1fa9cb8c7997c8c4d3d251fb5e41f749?s=128

Realm

October 08, 2015
Tweet

Transcript

  1. 4.

    A Teakettle • That requires you to stand near it

    • And watch it in order for it to boil water
  2. 9.

    Overview • How to download in the foreground • How

    to make that request background compatible • How to navigate common pitfalls
  3. 11.

    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. 12.

    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. 13.

    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. 14.

    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. 19.
  8. 20.

    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. 22.

    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() }
  10. 28.

    Life of a background request • Requests start in foreground

    • App dies • All requests in session finish in background • App starts back up in background
  11. 30.

    Recreating Tasks The systems knows which tasks to recreate by

    the session configuration identifier alone.
  12. 33.
  13. 38.

    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
  14. 40.

    Recipe • Create custom session with background configuration • Move

    code from completion handlers to delegate methods
  15. 42.
  16. 47.
  17. 49.
  18. 51.

    Recipe • Create custom session with background configuration • Move

    code from completion handlers to delegate methods • Persist any information needed for request callback
  19. 52.
  20. 54.
  21. 55.

    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
  22. 56.