アプリが動いたその先へ
 (iOS アプリ開発編)

アプリが動いたその先へ
 (iOS アプリ開発編)

6a55a7fb19c1fa78e86ddca7a69db088?s=128

Yuka Ezura

August 18, 2017
Tweet

Transcript

  1. 2.

    “\(self)” • iOS engineer @ LINE • ֶੜ࣌: Android, iOS

    engineer • Swift, Objective-C, Kotlin, Java, … ezura (@eduraaa)
  2. 8.

    ॳ৺ऀͷͱ͖ٙ໰ͩͬͨ͜ͱɺ೰ΜͰͨ͜ͱ
 Α͘ฉ࣭͘໰ weak ͱ strong ͷ࢖͍෼͚ thread ؅ཧͬͯ
 ݁ہͲ͏͢Ε͹ྑ͍ͷʁ งғؾͰ

    thread Λ
 ࢖͍ͬͯΔ ॥؀ࢀরා͍
 Ͳ͏͢Ε͹๷͛Δͷʁ ࣗ෼ͷϓϩάϥϜͷ඼࣭ͬͯ
 ࣮ࡍʹͲͷϨϕϧͳͷ… ຊ౰͸μϝͳ͜ͱΛ
 ΍͍ͬͯΔͷͰ͸… storyboard ͸
 ࢖ͬͨํ͕ྑ͍ͷʁ
  3. 23.

    ϝϞϦͷ؅ཧ • Java, Ruby, JavaScript, … • ํ๏: Garbage Collection

    • Ͳ͔͜Β΋ࢀর͞Εͳ͘ͳͬͨΦϒδΣΫτΛ GC ͕ௐ΂ͯղ์
 (ਓ͕؅ཧ͠ͳ͍) • Swift, Objective-C • ํ๏: Reference Counting • ඞཁͳΒΧ΢ϯτΛ૿΍͠ɺཁΒͳ͘ͳͬͨΒΧ΢ϯτΛݮΒ͢
 Χ΢ϯτ͕ 0 ʹͳͬͨΒෆཁͱΈͳͯ͠ղ์
 (Reference Count = ΦϒδΣΫτΛඞཁͱ͍ͯ͠Δ෺ͷ਺)
  4. 24.

    Reference Counting • Swift, Objective-C • ARC (Automatic Reference Counting)

    • weak ࢀরͱ strong ࢀর • Objective-C (ੲ) • MRC (Manual Reference Counting) • [obj retain] ͱ͔ॻ͍ͯ͋ͬͨΒ͜Ε
  5. 25.

    Reference Counting • Swift, Objective-C • ARC (Automatic Reference Counting)

    • weak ࢀরͱ strong ࢀর • Objective-C (ੲ) • MRC (Manual Reference Counting) • [obj retain] ͱ͔ॻ͍ͯ͋ͬͨΒ͜Ε
  6. 28.

    Reference Count object strong let obj = object ࢀরΧ΢ϯτ -=

    1 
 => 0 • ΦϒδΣΫτΛ࡞Γ·ͨ͠ • strong ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • strong ࢀর͍ͯ͠Δม਺͕ͳ͘ͳΓ·ͨ͠
  7. 29.
  8. 32.

    Reference Count object strong let obj = object ࢀরΧ΢ϯτ 


    => 1 weak var obj = object • ΦϒδΣΫτΛ࡞Γ·ͨ͠ • strong ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • weak ࢀর͍ͯ͠Δม਺͕͋Γ·͢
  9. 33.

    Reference Count • ΦϒδΣΫτΛ࡞Γ·ͨ͠ • strong ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • weak ࢀর͍ͯ͠Δม਺͕͋Γ·͢

    • weak ࢀর͍ͯ͠Δม਺͕ͳ͘ͳΓ·ͨ͠ object strong let obj = object ࢀরΧ΢ϯτ 
 => 1 weak var obj = object
  10. 34.

    Reference Count • ΦϒδΣΫτΛ࡞Γ·ͨ͠ • strong ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • weak ࢀর͍ͯ͠Δม਺͕͋Γ·͢

    • weak ࢀর͍ͯ͠Δม਺͕ͳ͘ͳΓ·ͨ͠ object strong let obj = object ࢀরΧ΢ϯτ 
 => 1
  11. 35.

    Reference Count object strong let obj = object ࢀরΧ΢ϯτ +=

    1 
 => 1 weak var obj = object • ΦϒδΣΫτΛ࡞Γ·ͨ͠ • strong ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • weak ࢀর͍ͯ͠Δม਺͕͋Γ·͢
  12. 36.

    Reference Count object strong let obj = object ࢀরΧ΢ϯτ -=

    1 
 => 0 weak var obj = object • ΦϒδΣΫτΛ࡞Γ·ͨ͠ • strong ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • weak ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • strong ࢀর͍ͯ͠Δม਺͕ͳ͘ͳΓ·ͨ͠
  13. 37.

    Reference Count object strong let obj = object ࢀরΧ΢ϯτ
 =>

    0 weak var obj = object • ΦϒδΣΫτΛ࡞Γ·ͨ͠ • strong ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • weak ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • strong ࢀর͍ͯ͠Δม਺͕ͳ͘ͳΓ·ͨ͠ • ΦϒδΣΫτ͸ղ์͞Ε·͢
  14. 38.

    Reference Count object strong let obj = object ࢀরΧ΢ϯτ
 =>

    0 weak var obj = object • ΦϒδΣΫτΛ࡞Γ·ͨ͠ • strong ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • weak ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • strong ࢀর͍ͯ͠Δม਺͕ͳ͘ͳΓ·ͨ͠ • ΦϒδΣΫτ͸ղ์͞Ε·͢ ஫: nil ͕୅ೖ͞ΕΔΘ͚Ͱ͸ͳ͍
  15. 43.

    ࢀর strong var obj = objB objA strong var obj

    = objA objB Ͳ͏ͨ͠Βղ์͞ΕΔʁ
  16. 44.

    ࢀর strong var obj = nil objA strong var obj

    = objA objB ໌ࣔతʹࢀরΛղ͘ඞཁ͕͋Δ
  17. 45.

    ࢀর strong var obj = nil objA strong var obj

    = objA objB Auto ͱ͸Ұମ… :(
  18. 46.

    ࢀর strong var obj = nil objA strong var obj

    = objA objB ʮͺͬͱݟΘ͔Βͳ͍͚Ͳɺ~͠ͳ͍ͱ͍͚ͳ͍ʯ
 ͷΑ͏ͳɺ҉໧తྃղͷ͋Δίʔυ͸͍͔ͭഁ໓͢Δ
  19. 54.
  20. 55.
  21. 57.
  22. 59.

    ParentViewController ChildViewController strong var cvc = cvc weak var delegate

    = ChildViewController pvc ParentViewController ParentViewController ͕͍ͳ͘ͳͬͨΒɺ ChildViewController ΋͍Βͳ͘ͳΔؔ܎
  23. 74.

    ໰ 1 weak ʹ͢΂͖ʁ strong ʹ͢΂͖ʁ class ViewController: UIViewController {


    @IBOutlet /* weak? strong? */ var label: UILabel! … }
  24. 84.

    ໰ 1 view viewController view sub views weak memory warning

    ൃੜ࣌ʹ
 view Λഁغ͢Δ৔߹͕͋ΔͷͰɺ
 weak ͷํ͕޷౎߹ͳ৔߹͕ଟ͍
  25. 89.
  26. 92.

    DispatchQueue.main.async { // σʔλΛදࣔ } ஋Λઃఆ ViewController 
 ࡞੒ දࣔ

    • UIViewController ࡞੒ͯ͠ • ஋Λઃఆͯ͠ • ͦΕΛදࣔ • αʔό͔ΒσʔλΛͱ͖ͬͯͯ • σʔλΛදࣔ αʔόͱ௨৴ σʔλΛදࣔ thread thread
  27. 93.

    thread • thread of execution • single thread (Լਤ) ը૾Λ

    ը໘Λ੾Γସ͑Δ λοϓ͞Εͨ σʔλΛอଘ ड৴଴ͪ μ΢ϯϩʔυ࢝ΊΔ ड৴த εΫϩʔϧ
  28. 94.

    thread • thread of execution • multi thread (Լਤ) ը૾Λදࣔ͢Δ

    ը໘Λ੾Γସ͑Δ λοϓ͞Εͨ σʔλΛอଘ ड৴଴ͪ μ΢ϯϩʔυ࢝ΊΔ ड৴த εΫϩʔϧ
  29. 95.

    iOS Ͱͷૢ࡞ • GCD • NSOperation • ௚઀ thread Λѻ͏

    (͋·Γ΍Βͳ͍) • (Rx ͷϝιου౳Ͱࢦఆ) DispatchQueue.global().async { <#code#> }
  30. 96.

    iOS Ͱͷ thread • main thread • 1 ͚ͭͩ (ΞϓϦىಈ࣌ʹ࡞੒͞ΕΔ)

    • sub thread • ෳ਺ଘࡏՄೳ • ௨৴ॲཧɺDB ૢ࡞౳ͷॏ͍ॲཧ΍ϩάΛૹ৴͢ΔͳͲͷॲཧ
  31. 101.

    main thread • ΞϓϦىಈ࣌ʹ࡞੒͞ΕΔ • Run loop ͕ճ͍ͬͯΔ • ΠϕϯτΛϋϯυϦϯά

    • λοϓ͞ΕͨͳͲͷ UI Πϕϯτ • ϥΠϑαΠΫϧ (ىಈͨ͠ɺ෮ؼͨ͠ɺ…) • දࣔͷߋ৽ • …
  32. 110.

    main thread • ΞϓϦىಈ࣌ʹ࡞੒͞ΕΔ • Run loop ͕ճ͍ͬͯΔ • ΠϕϯτΛϋϯυϦϯά

    • λοϓ͞ΕͨͳͲͷ UI Πϕϯτ • ϥΠϑαΠΫϧ (ىಈͨ͠ɺ෮ؼͨ͠ɺ…) • දࣔͷߋ৽ • …
  33. 112.

    ໰ 1 @IBAction func buttonDidTap(_ sender: UIButton) { label.text =

    "Changed" label.textColor = .red sleep(2) view.backgroundColor = .green } 1.Label ͷ৭ͱςΩετΛมߋ 2.2 ඵؒ sleep 3.എܠ৭Λมߋ
  34. 113.

    ໰ 1 (౴͑) @IBAction func buttonDidTap(_ sender: UIButton) { label.text

    = "Changed" label.textColor = .red sleep(2) view.backgroundColor = .green } 1. 2 ඵؒ Ϣʔβͷૢ࡞Λड͚෇͚ͳ͍ 2. Label ͱ എܠͷද͕ࣔಉ࣌ʹมΘΔ
  35. 115.

    ໰ 2 @IBAction func buttonDidTap(_ sender: UIButton) { DispatchQueue.global().async {

    [label = label] in label?.text = "Changed" label?.textColor = .red } view.backgroundColor = .green } 1.sub thread Ͱ Label Λมߋ 2.main thread ͰഎܠΛมߋ
  36. 116.

    ໰ 2 (౴͑) @IBAction func buttonDidTap(_ sender: UIButton) { DispatchQueue.global().async

    { [label = label] in label?.text = "Changed" label?.textColor = .red } view.backgroundColor = .green } ෆఆͷಈ࡞
  37. 117.

    Πϕϯτ଴ͪ
 main loop ը໘ͷߋ৽ λοϓ͞Εͨ = Ϣʔβ༝དྷͷૢ࡞
 λοϓ͞Εͨͱ͖ͷॲཧ (func buttonDidTap)


    view ͷഎܠߋ৽ ໰ 2 (ղઆ) label ͷ text Λมߋ
 label ͷେ͖͞Λߋ৽
 ؔ࿈͢Δ view ͷ഑ஔΛ࠶ܭࢉ
 label ͷ৭Λมߋ
 … main thread sub thread
  38. 122.

    Ͳ͏͍͏ͱ͖ɺ
 ͦΕΛҧ൓ͯ͠͠·͏ͷʁ sub thread Ͱ UI Λૢ࡞͢ΔॲཧΛ࣮ߦͨ͠ͱ͖
 * UI Λૢ࡞͢ΔॲཧΛؚΜͰ͍Δ

    function Λ sub thread Ͱ࣮ߦ
 * UI Λૢ࡞͢ΔॲཧΛؚΉ closure Λ sub thread Ͱ࣮ߦ
 * didSet ౳ʹ UI Λૢ࡞͢ΔॲཧΛؚΜͰ͍ΔϓϩύςΟʹ sub thread Ͱ୅ೖॲཧ࣮ߦ
 * Rx Ͱɺ͍ͭͷؒʹ͔ sub thread Ͱ࣮ߦ͞ΕΔΑ͏ʹͳ͍ͬͯͨ
 ͳͲͳͲ
  39. 123.

    ๷͙ํ๏͸ʁ • UI ͷૢ࡞ΛؚΉ͜ͱΛ໊લ΍ίϝϯτͰ໌ࣔ͢Δ • sub thread Ͱ࣮ߦ͍ͯͯ͠ɺUI Λૢ࡞ͨ͘͠ͳͬͨΒɺGCD ౳Λ࢖ͬͯ


    main thread Ͱ࣮ߦ͢ΔΑ͏ʹ੍ޚ • Xcode9 Ͱ͸ UI ͷૢ࡞Λ sub thread ্Ͱ࣮ߦ͍ͯ͠Δͱܯࠂͯ͘͠ΕΔػೳ͕ ͋Δ
  40. 125.

    ·ͱΊ • ͋ΘͯͣɺجૅΛ਎ʹ͚ͭΔͱਊ͔Βڧ͘ͳΕΔ • weak, strong ͷ࢖͍෼͚ • ΦϒδΣΫτؒͷؔ܎ੑ͔Βߟ͑ͯΈΑ͏ •

    thread • main thread ্Ͱ UI ૢ࡞ • function ΍ closure ͸ݺͼग़͞Εͨ thread ্Ͱ࣮ߦ͞ΕΔͷΛ
 ೦಄ʹஔ͍ͯɺ࣮ߦ࣌ͷঢ়گΛ૝૾ͯ͠ΈΑ͏
  41. 126.

    Q&A