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

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

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

Yuka Ezura

August 18, 2017
Tweet

More Decks by Yuka Ezura

Other Decks in Programming

Transcript

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

    engineer • Swift, Objective-C, Kotlin, Java, … ezura (@eduraaa)
  2. ॳ৺ऀͷͱ͖ٙ໰ͩͬͨ͜ͱɺ೰ΜͰͨ͜ͱ
 Α͘ฉ࣭͘໰ weak ͱ strong ͷ࢖͍෼͚ thread ؅ཧͬͯ
 ݁ہͲ͏͢Ε͹ྑ͍ͷʁ งғؾͰ

    thread Λ
 ࢖͍ͬͯΔ ॥؀ࢀরා͍
 Ͳ͏͢Ε͹๷͛Δͷʁ ࣗ෼ͷϓϩάϥϜͷ඼࣭ͬͯ
 ࣮ࡍʹͲͷϨϕϧͳͷ… ຊ౰͸μϝͳ͜ͱΛ
 ΍͍ͬͯΔͷͰ͸… storyboard ͸
 ࢖ͬͨํ͕ྑ͍ͷʁ
  3. ϝϞϦͷ؅ཧ • Java, Ruby, JavaScript, … • ํ๏: Garbage Collection

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

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

    • weak ࢀরͱ strong ࢀর • Objective-C (ੲ) • MRC (Manual Reference Counting) • [obj retain] ͱ͔ॻ͍ͯ͋ͬͨΒ͜Ε
  6. Reference Count object strong let obj = object ࢀরΧ΢ϯτ -=

    1 
 => 0 • ΦϒδΣΫτΛ࡞Γ·ͨ͠ • strong ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • strong ࢀর͍ͯ͠Δม਺͕ͳ͘ͳΓ·ͨ͠
  7. Reference Count object strong let obj = object ࢀরΧ΢ϯτ 


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

    • weak ࢀর͍ͯ͠Δม਺͕ͳ͘ͳΓ·ͨ͠ object strong let obj = object ࢀরΧ΢ϯτ 
 => 1 weak var obj = object
  9. Reference Count • ΦϒδΣΫτΛ࡞Γ·ͨ͠ • strong ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • weak ࢀর͍ͯ͠Δม਺͕͋Γ·͢

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

    1 
 => 1 weak var obj = object • ΦϒδΣΫτΛ࡞Γ·ͨ͠ • strong ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • weak ࢀর͍ͯ͠Δม਺͕͋Γ·͢
  11. Reference Count object strong let obj = object ࢀরΧ΢ϯτ -=

    1 
 => 0 weak var obj = object • ΦϒδΣΫτΛ࡞Γ·ͨ͠ • strong ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • weak ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • strong ࢀর͍ͯ͠Δม਺͕ͳ͘ͳΓ·ͨ͠
  12. Reference Count object strong let obj = object ࢀরΧ΢ϯτ
 =>

    0 weak var obj = object • ΦϒδΣΫτΛ࡞Γ·ͨ͠ • strong ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • weak ࢀর͍ͯ͠Δม਺͕͋Γ·͢ • strong ࢀর͍ͯ͠Δม਺͕ͳ͘ͳΓ·ͨ͠ • ΦϒδΣΫτ͸ղ์͞Ε·͢
  13. Reference Count object strong let obj = object ࢀরΧ΢ϯτ
 =>

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

    = objA objB Ͳ͏ͨ͠Βղ์͞ΕΔʁ
  15. ࢀর strong var obj = nil objA strong var obj

    = objA objB ໌ࣔతʹࢀরΛղ͘ඞཁ͕͋Δ
  16. ࢀর strong var obj = nil objA strong var obj

    = objA objB Auto ͱ͸Ұମ… :(
  17. ࢀর strong var obj = nil objA strong var obj

    = objA objB ʮͺͬͱݟΘ͔Βͳ͍͚Ͳɺ~͠ͳ͍ͱ͍͚ͳ͍ʯ
 ͷΑ͏ͳɺ҉໧తྃղͷ͋Δίʔυ͸͍͔ͭഁ໓͢Δ
  18. ParentViewController ChildViewController strong var cvc = cvc weak var delegate

    = ChildViewController pvc ParentViewController ParentViewController ͕͍ͳ͘ͳͬͨΒɺ ChildViewController ΋͍Βͳ͘ͳΔؔ܎
  19. ໰ 1 weak ʹ͢΂͖ʁ strong ʹ͢΂͖ʁ class ViewController: UIViewController {


    @IBOutlet /* weak? strong? */ var label: UILabel! … }
  20. ໰ 1 view viewController view sub views weak memory warning

    ൃੜ࣌ʹ
 view Λഁغ͢Δ৔߹͕͋ΔͷͰɺ
 weak ͷํ͕޷౎߹ͳ৔߹͕ଟ͍
  21. DispatchQueue.main.async { // σʔλΛදࣔ } ஋Λઃఆ ViewController 
 ࡞੒ දࣔ

    • UIViewController ࡞੒ͯ͠ • ஋Λઃఆͯ͠ • ͦΕΛදࣔ • αʔό͔ΒσʔλΛͱ͖ͬͯͯ • σʔλΛදࣔ αʔόͱ௨৴ σʔλΛදࣔ thread thread
  22. thread • thread of execution • single thread (Լਤ) ը૾Λ

    ը໘Λ੾Γସ͑Δ λοϓ͞Εͨ σʔλΛอଘ ड৴଴ͪ μ΢ϯϩʔυ࢝ΊΔ ड৴த εΫϩʔϧ
  23. thread • thread of execution • multi thread (Լਤ) ը૾Λදࣔ͢Δ

    ը໘Λ੾Γସ͑Δ λοϓ͞Εͨ σʔλΛอଘ ड৴଴ͪ μ΢ϯϩʔυ࢝ΊΔ ड৴த εΫϩʔϧ
  24. iOS Ͱͷૢ࡞ • GCD • NSOperation • ௚઀ thread Λѻ͏

    (͋·Γ΍Βͳ͍) • (Rx ͷϝιου౳Ͱࢦఆ) DispatchQueue.global().async { <#code#> }
  25. iOS Ͱͷ thread • main thread • 1 ͚ͭͩ (ΞϓϦىಈ࣌ʹ࡞੒͞ΕΔ)

    • sub thread • ෳ਺ଘࡏՄೳ • ௨৴ॲཧɺDB ૢ࡞౳ͷॏ͍ॲཧ΍ϩάΛૹ৴͢ΔͳͲͷॲཧ
  26. main thread • ΞϓϦىಈ࣌ʹ࡞੒͞ΕΔ • Run loop ͕ճ͍ͬͯΔ • ΠϕϯτΛϋϯυϦϯά

    • λοϓ͞ΕͨͳͲͷ UI Πϕϯτ • ϥΠϑαΠΫϧ (ىಈͨ͠ɺ෮ؼͨ͠ɺ…) • දࣔͷߋ৽ • …
  27. main thread • ΞϓϦىಈ࣌ʹ࡞੒͞ΕΔ • Run loop ͕ճ͍ͬͯΔ • ΠϕϯτΛϋϯυϦϯά

    • λοϓ͞ΕͨͳͲͷ UI Πϕϯτ • ϥΠϑαΠΫϧ (ىಈͨ͠ɺ෮ؼͨ͠ɺ…) • දࣔͷߋ৽ • …
  28. ໰ 1 @IBAction func buttonDidTap(_ sender: UIButton) { label.text =

    "Changed" label.textColor = .red sleep(2) view.backgroundColor = .green } 1.Label ͷ৭ͱςΩετΛมߋ 2.2 ඵؒ sleep 3.എܠ৭Λมߋ
  29. ໰ 1 (౴͑) @IBAction func buttonDidTap(_ sender: UIButton) { label.text

    = "Changed" label.textColor = .red sleep(2) view.backgroundColor = .green } 1. 2 ඵؒ Ϣʔβͷૢ࡞Λड͚෇͚ͳ͍ 2. Label ͱ എܠͷද͕ࣔಉ࣌ʹมΘΔ
  30. ໰ 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 ͰഎܠΛมߋ
  31. ໰ 2 (౴͑) @IBAction func buttonDidTap(_ sender: UIButton) { DispatchQueue.global().async

    { [label = label] in label?.text = "Changed" label?.textColor = .red } view.backgroundColor = .green } ෆఆͷಈ࡞
  32. Πϕϯτ଴ͪ
 main loop ը໘ͷߋ৽ λοϓ͞Εͨ = Ϣʔβ༝དྷͷૢ࡞
 λοϓ͞Εͨͱ͖ͷॲཧ (func buttonDidTap)


    view ͷഎܠߋ৽ ໰ 2 (ղઆ) label ͷ text Λมߋ
 label ͷେ͖͞Λߋ৽
 ؔ࿈͢Δ view ͷ഑ஔΛ࠶ܭࢉ
 label ͷ৭Λมߋ
 … main thread sub thread
  33. Ͳ͏͍͏ͱ͖ɺ
 ͦΕΛҧ൓ͯ͠͠·͏ͷʁ sub thread Ͱ UI Λૢ࡞͢ΔॲཧΛ࣮ߦͨ͠ͱ͖
 * UI Λૢ࡞͢ΔॲཧΛؚΜͰ͍Δ

    function Λ sub thread Ͱ࣮ߦ
 * UI Λૢ࡞͢ΔॲཧΛؚΉ closure Λ sub thread Ͱ࣮ߦ
 * didSet ౳ʹ UI Λૢ࡞͢ΔॲཧΛؚΜͰ͍ΔϓϩύςΟʹ sub thread Ͱ୅ೖॲཧ࣮ߦ
 * Rx Ͱɺ͍ͭͷؒʹ͔ sub thread Ͱ࣮ߦ͞ΕΔΑ͏ʹͳ͍ͬͯͨ
 ͳͲͳͲ
  34. ๷͙ํ๏͸ʁ • UI ͷૢ࡞ΛؚΉ͜ͱΛ໊લ΍ίϝϯτͰ໌ࣔ͢Δ • sub thread Ͱ࣮ߦ͍ͯͯ͠ɺUI Λૢ࡞ͨ͘͠ͳͬͨΒɺGCD ౳Λ࢖ͬͯ


    main thread Ͱ࣮ߦ͢ΔΑ͏ʹ੍ޚ • Xcode9 Ͱ͸ UI ͷૢ࡞Λ sub thread ্Ͱ࣮ߦ͍ͯ͠Δͱܯࠂͯ͘͠ΕΔػೳ͕ ͋Δ
  35. ·ͱΊ • ͋ΘͯͣɺجૅΛ਎ʹ͚ͭΔͱਊ͔Βڧ͘ͳΕΔ • weak, strong ͷ࢖͍෼͚ • ΦϒδΣΫτؒͷؔ܎ੑ͔Βߟ͑ͯΈΑ͏ •

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