Ash Furrow, Artsy
(on iOS)
Evolution of
Asynchronous
Programming
Slide 2
Slide 2 text
Evolution of Async Programming
Slide 3
Slide 3 text
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
Slide 4
Slide 4 text
(•_•)
<) )╯
/ \
\(•_•)
( (>
/ \
(•_•)
<) )>
/ \
Decisions
are made
in a context
Slide 5
Slide 5 text
What’s a context?
Slide 6
Slide 6 text
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?
Slide 7
Slide 7 text
Decisions aren’t made in a vacuum
Slide 8
Slide 8 text
Unique, in space…
and time
Slide 9
Slide 9 text
Adopting a codebase
Slide 10
Slide 10 text
Context includes experience
Slide 11
Slide 11 text
But you don’t have to
You can never understand
the context a decision was made in
Slide 12
Slide 12 text
It is enough to appreciate that a
context existed
Slide 13
Slide 13 text
Be empathetic
Slide 14
Slide 14 text
iOS developers conflate things
⽌ •́ ﹏ •̀ ⽎
Slide 15
Slide 15 text
conflate
verb [ with obj. ]
combine (two or more ideas,
texts, etc.) into one.
Slide 16
Slide 16 text
Humans look for patterns
Slide 17
Slide 17 text
No content
Slide 18
Slide 18 text
No content
Slide 19
Slide 19 text
What does this have to do with
iOS development?
Slide 20
Slide 20 text
Some popular development practice
When learning iOS, we’re bad at that practice
Popular development practice is bad
+
=
Slide 21
Slide 21 text
We get better at iOS development
New thing comes out, and we’re good at it
New thing is good
+
=
Slide 22
Slide 22 text
This is why new things are
always so appealing
Slide 23
Slide 23 text
Why do iOS devs do this?
There are several explanations
Slide 24
Slide 24 text
iOS is young
… really young
Slide 25
Slide 25 text
Always older APIs for us to blame
iOS is constantly changing
Slide 26
Slide 26 text
I’ve done this a lot
You probably have, too
Slide 27
Slide 27 text
Only human
Slide 28
Slide 28 text
Remember…
Don’t be hard on yourself!
Slide 29
Slide 29 text
And don’t be hard on others!
We’re all just winging it!
Blocks / GCD
• iOS 4+
• Super-easy
• Super-dangerous (reference cycles)
• Better in Swift
• Not cancellable
Slide 39
Slide 39 text
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?
Slide 40
Slide 40 text
NSProgress
• iOS 7+
• Confusing
• No one knows what it’s for
• I don’t know
• Do you know?
Slide 41
Slide 41 text
NSURLSessionTask
• iOS 7+
• Network-specific async abstraction
• Very nice
• Seriously, very nice
• (Will discuss more later)
Slide 42
Slide 42 text
Let’s build something
Those are only tools
Slide 43
Slide 43 text
Abstraction
Blocks
Function pointers
NSOperation
???
Slide 44
Slide 44 text
Example Abstractions
• Adding block callbacks to UIGestureRecognizer, UIAlertView, etc
• Futures, promises
• Sequences, signals (FRP)
Slide 45
Slide 45 text
Those are great abstractions, but…
I feel we ignore NSOperation
Slide 46
Slide 46 text
NSOperation
• Cancellable
• Chainable, dependency graphs
• Concurrency support
• Completion handlers
• Prioritization
• Interoperate with GCD
Slide 47
Slide 47 text
Why so GCDerious?
Slide 48
Slide 48 text
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
Slide 49
Slide 49 text
No content
Slide 50
Slide 50 text
Different solutions work for
different problems
Slide 51
Slide 51 text
Get familiar with your options
Slide 52
Slide 52 text
Don’t be mean to developers
who disagree with you
Slide 53
Slide 53 text
There are benefits to older solutions
(╯°□°)╯︵ ┻━┻
Slide 54
Slide 54 text
Developers (like me ) tend to say
Objective-C is “old and busted”
Slide 55
Slide 55 text
Old, yes, but not busted
Slide 56
Slide 56 text
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
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!
Slide 61
Slide 61 text
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
Slide 62
Slide 62 text
Try to be more like
NSURLSession
Slide 63
Slide 63 text
Go read NSOperation.h
Slide 64
Slide 64 text
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
Slide 65
Slide 65 text
So what’s the best way to do
asynchronous programming on iOS?