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

iOSで無限バックグラウンドアップロード(に挑戦してみた話)

ainame
March 12, 2015

 iOSで無限バックグラウンドアップロード(に挑戦してみた話)

ainame

March 12, 2015
Tweet

More Decks by ainame

Other Decks in Programming

Transcript

  1. i O S Ͱ ແ ݶ ό ο Ϋ ά ϥ ΢ ϯ υ Ξ ο
    ϓ ϩ ʔ υ ʢ ʹ ௅ ઓ ͯ͠ Έ ͨ ࿩ ʣ
    P o t a t o t i p s # 1 5 2 0 1 5 - 0 3 - 1 2

    View full-size slide

  2. ஫ ҙ ʢ ௥ ه ʣ
    • ࠓճͷൃද͸iPhone/iOS/APNSͳͲʹυΩϡϝϯτʹ
    ͸هࡌ͞Ε͍ͯͳ͍ϒϥοΫϘοΫεͳ࢓༷ʹ͍ͭͯ
    ະݕূͷ෦෼͕͋ΔͷͰਖ਼֬Ͱ͸ͳ͍Ͱ͢
    • ൃදޙࢦఠ͍͍ͨͩͨͷͰҰ෦मਖ਼͠·ͨ͠

    View full-size slide

  3. @ a i n a m e
    R u b y / A W S / i O S
    ࣗ ݾ ঺ հ

    View full-size slide

  4. Α ͘ ͋ Δ Ξϧό Ϝ Ξ ϓ Ϧ
    • ίϝϯτΛ͚ͭͯࣸਅΛΞοϓϩʔυʢ1ճ1ຕʣ
    • ϐοΧʔͰը૾Λબ୒ͯ͠ΔͱݱΕΔʮˎҰ౓ʹΞο
    ϓϩʔυग़དྷΔຕ਺͸Nຕ·ͰͰ͢ʯ
    • ϑΥΞάϥ΢ϯυͰͷΞοϓϩʔυ͕લఏ

    View full-size slide

  5. ࡞ Γ ͨ ͍ Ξϧό Ϝ Ξ ϓ Ϧ
    • ϐοΧʔͰը૾Λࢦఆ͢ΔͱΞοϓϩʔυ͕࢝·Δ
    • ΞϓϦΛดͯ͡΋όοΫάϥ΢ϯυͰ׬ྃͤ͞Δ
    • όοΫάϥ΢ϯυͰແݶʹΞοϓϩʔυ

    View full-size slide

  6. ՝ ୊
    • iOSΞϓϦ͸όοΫάϥ΢ϯυॲཧ͕ωοΫ
    • 3෼ͷ੍ݶ͕͋Δ
    • iOS7Ͱ௥Ճ͞ΕͨNSURLSessionͷbackgroundػೳ࢖͑ͦ͏
    • ӬଓԽ͞ΕͨJobΩϡʔ͕ඞཁͦ͏
    • NSOperationQueueͩͱϝϞϦʹ͔͠σʔλͷΒͳ͍ͷͰ
    ΞϓϦดͨ͡Βࢮ͵

    View full-size slide

  7. ό ο Ϋ ά ϥ ΢ ϯ υ Ͱ ௨ ৴
    • NSURLSessionΛҎԼΛ૊Έ߹Θͤͯ࢖͏
    • NSURLSessionConfiguration
    backgroundSessionConfigurationWithIdentifier:
    • NSURLSession uploadTaskWithRequest:fromFile:
    • Ҿ਺ʹNSURLͰ౉ͨ͠ϑΝΠϧύεͷϑΝΠϧΛHTTPϦΫΤε
    τͷBodyʹ٧Ίͯapplication/octet-streamͰPOST
    • όοΫάϥ΢ϯυͰ΋Ξοϓϩʔυॲཧ͕ܧଓՄೳΒ͠
    ͍ɾɾɾʁ

    View full-size slide

  8. Ӭ ଓ Խ J O B Ω ϡ ʔ
    • EDQueueͱ͍͏ϥΠϒϥϦΛࢀߟʹಠ࣮ࣗ૷
    • https://github.com/thisandagain/queue
    • FMDB+SQLiteͰӬଓԽ
    • લͷJob͕ऴྃͨ͠Β࣍ͷJobΛऔΓग़࣮ͯ͠ߦ͢Δ

    View full-size slide

  9. ࣮ ࡍ ʹ ࢼ ͯ͠ Έ ͨ
    • ͔֬ʹΞοϓϩʔυग़དྷΔɾɾɾʂ
    • ͍͢͝ɾɾɾʂ
    • ΍ͬͨɾɾɾʂ׬੒΍ɾɾɾʂ
    • ͔͠͠ɺDeploygateܦ༝Ͱ࢖ͬͯ΋Β͏ͱಈ͔ͳ
    ͍ɾɾɾʂʁʂʁʂʁ

    View full-size slide

  10. ໰ ୊ ൃ ੜ
    • NSURLSessionConfiguration discretionaryͷυΩϡϝϯτΛಡΜͰΈΔ
    • > When transferring large amounts of data, you are encouraged to
    set the value of this property to YES. Doing so lets the system
    schedule those transfers at times that are more optimal for the
    device. For example, the system might delay transferring large files
    until the device is plugged in and connected to the network via Wi-
    Fi. The default value of this property is NO.
    • ཁ໿: ୺຤͕Lightningέʔϒϧʹܨ͕͍ͬͯͯWi-FiΛར༻͢Δ·Ͱ
    େ͖ͳϑΝΠϧͷసૹΛ଴ͭ
    • https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionConfiguration_class/
    index.html#//apple_ref/occ/instp/NSURLSessionConfiguration/discretionary

    View full-size slide

  11. ࠶ ࣮ ૷
    • Silent Remote NotificationΛར༻͢Δ
    • {aps: {content-available:1}}Λ͚ͭΔ
    • Push௨஌Λड͚औΔͱ30ඵఔ౓όοΫάϥ΢ϯυͰ
    ॲཧ͕࣮ߦͰ͖Δ
    • Ξοϓϩʔυ -> Push௨஌-> Ξοϓϩʔυ -> …

    Ͱ͏·͍͘͘ͷͰ͸ɾɾɾʁ

    View full-size slide

  12. Ϋ ϥ Π Ξ
    ϯ τ
    α ʔόʔ
    ը ૾

    View full-size slide

  13. Ϋ ϥ Π Ξ
    ϯ τ
    α ʔόʔ
    P U S H ௨ ஌

    View full-size slide

  14. · ͱ Ί
    • iOSͰόοΫάϥ΢ϯυॲཧʹ͸੍ݶ͕͋Δ
    • Background Transfer͸ʢ࢖͍͜ͳ͢ͷʣ೉͍͠
    • ҰఆִؒͰSilent Remote NotificationૹΕ͹

    ແݶΞοϓϩʔυ΋Մೳ!!!
    • ࣮ࡍʹ͸Push௨஌ʹ͸ඇެ։࢓༷ͰϨʔτϦϛοτ
    ͕͋ΔͷͰແݶͰ͸ͳ͍ͱͷ͜ͱཁݕূʢ௥هʣ

    View full-size slide