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

Evolution of Asynchronous Programming on iOS

Ash Furrow
October 02, 2015

Evolution of Asynchronous Programming on iOS

Presented at Mobiconf 2015: http://2015.mobiconf.org

Ash Furrow

October 02, 2015
Tweet

More Decks by Ash Furrow

Other Decks in Programming

Transcript

  1. Ash Furrow, Artsy
    (on iOS)
    Evolution of
    Asynchronous
    Programming

    View Slide

  2. Evolution of Async Programming

    View Slide

  3. Agenda
    • All decisions are made in a context
    • iOS developers are human; we tend to conflate things
    • Older solutions have value, too
    • There are benefits to using older technology

    View Slide

  4. (•_•)
    <) )╯
    / \
    \(•_•)
    ( (>
    / \
    (•_•)
    <) )>
    / \
    Decisions
    are made
    in a context

    View Slide

  5. What’s a context?

    View Slide

  6. Contextual Questions
    • What technology is available?
    • How much do I understand the options? What about my team?
    • How popular is the technology?
    • How stable are the tools I would need to use?
    • How easy is it to learn?
    • How quickly do I need to complete the task?

    View Slide

  7. Decisions aren’t made in a vacuum

    View Slide

  8. Unique, in space…
    and time

    View Slide

  9. Adopting a codebase

    View Slide

  10. Context includes experience

    View Slide

  11. But you don’t have to
    You can never understand
    the context a decision was made in

    View Slide

  12. It is enough to appreciate that a
    context existed

    View Slide

  13. Be empathetic

    View Slide

  14. iOS developers conflate things
    ⽌ •́ ﹏ •̀ ⽎

    View Slide

  15. conflate
    verb [ with obj. ]
    combine (two or more ideas,
    texts, etc.) into one.

    View Slide

  16. Humans look for patterns

    View Slide

  17. View Slide

  18. View Slide

  19. What does this have to do with
    iOS development?

    View Slide

  20. Some popular development practice
    When learning iOS, we’re bad at that practice
    Popular development practice is bad
    +
    =

    View Slide

  21. We get better at iOS development
    New thing comes out, and we’re good at it
    New thing is good
    +
    =

    View Slide

  22. This is why new things are
    always so appealing

    View Slide

  23. Why do iOS devs do this?
    There are several explanations

    View Slide

  24. iOS is young
    … really young

    View Slide

  25. Always older APIs for us to blame
    iOS is constantly changing

    View Slide

  26. I’ve done this a lot
    You probably have, too

    View Slide

  27. Only human

    View Slide

  28. Remember…
    Don’t be hard on yourself!

    View Slide

  29. And don’t be hard on others!
    We’re all just winging it!

    View Slide

  30. ⊂_ヽ

      \\ Λ_Λ

       \( ˇωˇ)

       / ⌒

      / へ\

      /  / \\

    ƀɹϊɹɹ ヽ_つ

      / /

     ( (ヽ

     | |、 \

     | ⼃丿 \

     | |  ) /

    ノ )  Lů
    Older solutions have merit

    View Slide

  31. –Lots of people, for hundreds of years
    “Those who don’t study history are
    doomed to repeat it.”

    View Slide

  32. Wrong

    View Slide

  33. –Me, just now
    “Those who don’t study the past can’t make
    informed decisions about the present.”

    View Slide

  34. History of Async Programming

    View Slide

  35. performSelector: Family
    • iOS 2+
    • Easy(ish)
    • Supports delays
    • Cancellable(ish)
    • Threadsafe(ish)

    View Slide

  36. NSThread
    • iOS 2+
    • Difficult, mostly
    • Sometimes, kind of cancellable
    • Complicated

    View Slide

  37. NSOperation
    • iOS 2+
    • Supports concurrency
    • Supports dependency graphs
    • Cancellable
    • More boilerplate

    View Slide

  38. Blocks / GCD
    • iOS 4+
    • Super-easy
    • Super-dangerous (reference cycles)
    • Better in Swift
    • Not cancellable

    View Slide

  39. NSOperation + Blocks
    • iOS 4+
    • Everything from NSOperation, but also with blocks
    • Less boilerplate
    • Slightly more verbose than blocks (this is Objective-C, remember)
    • Not popular – why?

    View Slide

  40. NSProgress
    • iOS 7+
    • Confusing
    • No one knows what it’s for
    • I don’t know
    • Do you know?

    View Slide

  41. NSURLSessionTask
    • iOS 7+
    • Network-specific async abstraction
    • Very nice
    • Seriously, very nice
    • (Will discuss more later)

    View Slide

  42. Let’s build something
    Those are only tools

    View Slide

  43. Abstraction
    Blocks
    Function pointers
    NSOperation
    ???

    View Slide

  44. Example Abstractions
    • Adding block callbacks to UIGestureRecognizer, UIAlertView, etc
    • Futures, promises
    • Sequences, signals (FRP)

    View Slide

  45. Those are great abstractions, but…
    I feel we ignore NSOperation

    View Slide

  46. NSOperation
    • Cancellable
    • Chainable, dependency graphs
    • Concurrency support
    • Completion handlers
    • Prioritization
    • Interoperate with GCD

    View Slide

  47. Why so GCDerious?

    View Slide

  48. Why GCD?
    • iOS devs favour GCD over other suitable abstractions
    • Why?
    • NSOperation is unfamiliar, feels unnecessary
    • Not-invented-here-syndrome
    • Apple is good at marketing

    View Slide

  49. View Slide

  50. Different solutions work for
    different problems

    View Slide

  51. Get familiar with your options

    View Slide

  52. Don’t be mean to developers
    who disagree with you

    View Slide

  53. There are benefits to older solutions
    (╯°□°)╯︵ ┻━┻

    View Slide

  54. Developers (like me ) tend to say
    Objective-C is “old and busted”

    View Slide

  55. Old, yes, but not busted

    View Slide

  56. Older Technology
    • “Old” is stable
    • “Old” is usually well-documented
    • “Old” is easy to hire for
    • “Old” has a lot of helpful resources, libraries, tutorials, etc

    View Slide

  57. AFNetworking uses
    NSOperation

    View Slide

  58. Let’s look at another example

    View Slide

  59. NSURLSession
    • Very protocol-oriented
    • NSURLSessionDelegate
    • NSURLSessionTaskDelegate
    • NSURLSessionDataDelegate
    • NSURLSessionDownloadDelegate

    View Slide

  60. NSURLSession
    • Separate delegates isolate different areas of concern
    • One object may conform to all protocols
    • Many, small protocols are better than one massive one
    • Swift protocols don’t even support optional functions!

    View Slide

  61. NSURLSession
    • Could have been completely blocks-based…
    • Instead, based on ideas behind NSOperation
    • NSURLSession probably wraps an NSOperation, anyway
    • This is good! No leaky abstractions

    View Slide

  62. Try to be more like
    NSURLSession

    View Slide

  63. Go read NSOperation.h

    View Slide

  64. Wrap Up
    • Decisions are made in an unknowable context
    • Developers are bad at evaluating things
    • New and shiny things aren’t always better
    • Old solutions carry extrinsic benefits

    View Slide

  65. So what’s the best way to do
    asynchronous programming on iOS?

    View Slide

  66. It depends.
    ¯\_(ツ)_/¯

    View Slide