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

Concurrency in iOS: #iOScon 2016

Ddd6d3bac7772fa67fc5e312a18bdaec?s=47 sammyd
May 26, 2016

Concurrency in iOS: #iOScon 2016

What is concurrency? Why use it? Why is so bloody hard?

Learn about using concurrency on Apple's platforms, and how you can improve your app by taking full advantage of the device.

Presented at #iOScon 2016.

Ddd6d3bac7772fa67fc5e312a18bdaec?s=128

sammyd

May 26, 2016
Tweet

Transcript

  1. Concurrency on iOS Sam Davies @iwantmyrealname _ github.com/sammyd/iOSConcurrency

  2. None
  3. concurrency is hard

  4. what is concurrency?

  5. why use concurrency?

  6. common concurrency problems

  7. race condition

  8. Race Condition time Value 1 Thread 1 Thread 2 inc

  9. Race Condition time Value 1 1 Thread 1 Thread 2

    inc r1
  10. Race Condition time Value 1 1 1 Thread 1 Thread

    2 inc r1 +1
  11. Race Condition time Value 1 1 1 2 Thread 1

    Thread 2 inc r1 w2 +1
  12. Race Condition time Value 1 1 1 2 2 Thread

    1 Thread 2 inc r1 w2 +1 inc
  13. Race Condition time Value 1 1 1 2 2 2

    Thread 1 Thread 2 inc r1 w2 +1 inc r2
  14. Race Condition time Value 1 1 1 2 2 2

    2 Thread 1 Thread 2 inc r1 w2 +1 inc r2 +1
  15. Race Condition time Value 1 1 1 2 2 2

    2 3 Thread 1 Thread 2 inc r1 w2 +1 inc r2 w3 +1
  16. Race Condition time Value 1 Thread 1 Thread 2 inc

  17. Race Condition time Value 1 1 Thread 1 Thread 2

    inc r1 inc
  18. Race Condition time Value 1 1 1 Thread 1 Thread

    2 inc r1 inc r1
  19. Race Condition time Value 1 1 1 1 Thread 1

    Thread 2 inc r1 inc r1 +1
  20. Race Condition time Value 1 1 1 2 1 Thread

    1 Thread 2 inc r1 inc r1 w2 +1
  21. Race Condition time Value 1 1 1 2 1 2

    Thread 1 Thread 2 inc r1 +1 inc r1 w2 +1
  22. Race Condition time Value 1 1 1 2 1 2

    2 Thread 1 Thread 2 inc r1 w2 +1 inc r1 w2 +1
  23. priority inversion

  24. Priority Inversion time low priority resource

  25. Priority Inversion time low priority resource

  26. Priority Inversion time low medium priority resource

  27. Priority Inversion time low medium high priority resource

  28. Priority Inversion time low medium high priority resource

  29. Priority Inversion time low medium high priority resource

  30. Priority Inversion time low medium high priority resource

  31. deadlock

  32. Deadlock time resource1 resource2 Thread 1 Thread 2

  33. Deadlock time resource1 resource2 Thread 1 Thread 2

  34. Deadlock time resource1 resource2 Thread 1 Thread 2

  35. Deadlock time resource1 resource2 Thread 1 Thread 2

  36. Deadlock time resource1 resource2 Thread 1 Thread 2

  37. Deadlock time resource1 resource2 Thread 1 Thread 2

  38. Deadlock time resource1 resource2 Thread 1 Thread 2

  39. Deadlock time resource1 resource2 Thread 1 Thread 2

  40. $&'( queueueueueue paradigm

  41. Tasks & Queues Thread 1 Thread 2 Queue

  42. Tasks & Queues Thread 1 Thread 2 Queue

  43. Tasks & Queues Thread 1 Thread 2 Queue

  44. Tasks & Queues Thread 1 Thread 2 Queue

  45. Tasks & Queues Thread 1 Thread 2 Queue

  46. Tasks & Queues Thread 1 Thread 2 Queue

  47. Tasks & Queues Thread 1 Thread 2 Queue

  48. feeling brave?

  49. a touch more theory

  50. Thread Safety time Value 1111 Thread 1 Thread 2 Thread

    3
  51. Thread Safety time Value 1111 Thread 1 Thread 2 r1111

    Thread 3
  52. Thread Safety time Value 1111 1112 Thread 1 Thread 2

    r1111 w2222 Thread 3
  53. Thread Safety time Value 1111 1112 1122 Thread 1 Thread

    2 r1111 w2222 r1122 Thread 3
  54. Thread Safety time Value 1111 1112 1122 1223 Thread 1

    Thread 2 r1111 w2222 r1122 w3333 Thread 3 r1223
  55. Thread Safety time Value 1111 1112 1122 1223 2233 Thread

    1 Thread 2 r1111 w2222 r1122 w3333 Thread 3 r1223
  56. Thread Safety time Value 1111 1112 1122 1223 2233 2333

    Thread 1 Thread 2 r1111 w2222 r1122 r2333 w3333 Thread 3 r1223
  57. Thread Safety time Value 1111 1112 1122 1223 2233 2333

    3333 Thread 1 Thread 2 r1111 w2222 r1122 r2333 w3333 Thread 3 r1223
  58. Thread Safety time Value 1111 1112 1122 1223 2233 2333

    3333 3333 3333 Thread 1 Thread 2 r1111 w2222 r1122 r2333 w3333 Thread 3 r1223 r3333 r3333
  59. Dispatch Barrier time task task task task task barrier task

    task task task task task task task
  60. Dispatch Barrier time read read read read read read write

    read read read read read read
  61. alternatives to 

  62. promises networkRequest(url, callback: { data in decompressor(data, callback: { image

    in imageResizer(image, callback: { small in filterImage(small, callback: { filtered in displayImage(filtered) }) }) }) })
  63. promises promise<int> int error

  64. promises str

  65. promises promise<img> str f

  66. promises promise<img> str error f img

  67. promises promise<img> str error f img error handler

  68. promises promise<img> str error f img g promise<img> error handler

  69. promises promise<img> str error f img g promise<img> error error

    handler img
  70. promises promise<img> str error f img g promise<img> error error

    handler img
  71. promises networkRequest(url) .then(decompressor) .then(imageResizer) .then({ (image) in return filterImage(image) }).then(displayImage)

    .catch({ (error) in print(error) })
  72. imagine the “promise pipeline” accepted a sequence of values…

  73. that’s reactive programming

  74. testing

  75. don’t do it

  76. test core, synchronous logic

  77. let libraries handle concurrency

  78. conclusion

  79. concurrency is hard

  80. you will get it wrong

  81. • raywenderlich.com/videos • github.com/sammyd/iOSConcurrency • @iwantmyrealname