watchOS におけるバックグラウンドタスクの限界

14.4K Views

September 12, 22

スライド概要

iOSDC 2022 で発表した際の資料です。

https://fortee.jp/iosdc-japan-2022/proposal/0970265f-664c-4626-848e-4c86f1efa94f

profile-image

iOS エンジニアのような何か

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

ダウンロード

関連スライド

各ページのテキスト
1.

iOSDC 2022 watchOS における バックグラウンドタスクの限界 @yamannnu

2.

はじめに 1 watchOS におけるバックグラウンドでの実行方法 以下の3つ フォアグラウンドの作業を続ける 1. Background Session 2. Extended Runtime Session スケジュールされた作業を行う 3. WKRefreshBackgroundTask それぞれの詳細と実装 Tips について解説していきます

3.

Background Session

4.

Background Session 3 概要 ・フォアグラウンドの作業をバックグラウンドで続けることができる ・できることは少ないが、実行時間が非常に長い ・3つの Mode がある ・watchOS 2 ~ なので、全ての Apple Watch が対象

5.

Background Session 4 Mode の内訳 Audio Location updates Workout processing Watch 4:21 閃光 exandros] - 閃光 - Sing 11:51 到着予定 4:21 375 m ここから出発 4:21 00:01.71 一拍/分 0.0 平均速度 KM/H 0 M上昇 高度 0M

6.

Background Session 5 実装 Tips (1/2) ※ Audio は AVAudioSession、Location updates は CLLocationManager、 Workout Processing は HKWorkoutSession を使う ※ セッションの開始はフォアグラウンドで行う必要がある ※ CPU 使用率が高いとシステム側に止められる

7.

Background Session 6 実装 Tips (2/2) ※ Background Modes の設定が必要 General Signing & Capabilities Resource Tags Info Build Settings Build Phases Build Rules PROJECT watch-background-modes TARGETS watch-background-modes watch-background-modesTests watch-background-modesUITests watch-background-modes WatchKit App watch-background-modes WatchKit Extension watch-background-modes WatchKit AppTests watch-background-modes WatchKit AppUITests + All Debug Release > Signing Background Modes Modes Audio Location updates Remote notifications Workout processing Session Type None Add capabilities by clicking the "+" button above.

8.

Extended Runtime Session

9.

Extended Runtime Session 8 概要 ・フォアグラウンドの作業をバックグラウンドで続けることができる ・Background Session より処理の自由度は高いが、実行時間がやや短い ・Bluetoothデバイスとの通信、データの処理、サウンドやハプティクスの再生が可能 ・4つの Session Type がある ・watchOS 6 ~ なので、全ての Apple Watch が対象 初代 Apple Watch は watchOS 4.3.2 までらしいので、Series 1 以降のみ対象でした…申し訳ございません

10.

Extended Runtime Session 9 Session Type Runtime Schedulable Time limit Self care Mindfulness Physical therapy Smart alarm

11.

Extended Runtime Session 10 Session Type Runtime Schedulable Time limit Self care Frontmost 10 min Mindfulness Physical therapy Smart alarm 歯磨きなどの感情的な幸福や健康に焦点をおてた、 短時間の活動をサポートする ※ Frontmost とは ・フォアグラウンドで実行 ・制限時間かアプリ切り替えで終了

12.

Extended Runtime Session 11 Session Type Runtime Schedulable Time limit Self care Mindfulness Frontmost 60 min Physical therapy Smart alarm 瞑想の開始と終了をサポートする ※ Background Session で済むならそっちを使おう ・歩く瞑想なら Workout ・音声の再生で済むなら Audio

13.

Extended Runtime Session 12 Session Type Runtime Schedulable Time limit Self care Mindfulness Physical therapy Background 60 min Smart alarm ストレッチ、筋力強化、可動域のエクササイズを サポートする ※ 激しい運動なら Workout を使おう ※ Background はアプリ切り替えでも継続される

14.

Extended Runtime Session 13 Session Type Runtime Schedulable Time limit Self care Mindfulness Physical therapy Smart alarm Background 30 min 心拍数と動きをモニタリングする時間帯の スケジュールをサポート ※ スケジュール可能なのは36時間後まで

15.

Extended Runtime Session 14 Session Type Runtime Schedulable Time limit Self care Frontmost 10 min Mindfulness Frontmost 60 min Physical therapy Background 60 min Smart alarm Background 30 min

16.

Extended Runtime Session 15 実装 Tips (1/2) ※ 実装は WKExtendedRuntimeSession を使う ※ セッションは同時に1つまで ※ セッションの開始はフォアグラウンドで行う必要がある ※ CPU 使用率が高いとシステム側に止められる

17.

Extended Runtime Session 16 実装 Tips (2/2) ※ Session Type は Time limit 等の制約ではなく、アプリの使用目的で選ぶ ※ Session Type の設定が必要 General Signing & Capabilities Resource Tags Info Build Settings Build Phases Build Rules PROJECT watch-background-modes TARGETS watch-background-modes watch-background-modesTests watch-background-modesUITests watch-background-modes WatchKit App watch-background-modes WatchKit Extension watch-background-modes WatchKit AppTests watch-background-modes WatchKit AppUITests + All Debug Release > Signing Background Modes Modes Audio Location updates Remote notifications Workout processing Session Type None Add capabilities by clicking the "+" button above.

18.

WKRefreshBackgroundTask

19.

WKRefreshBackgroundTask 18 概要 ・スケジュールされた作業を行うことができる ・できることは多いが、実行時間が非常に短い ・Task が7種類ある ・1つだけ watchOS 9 ~ だが、他は 5 あれば使えるので、大体の Apple Watch が対象

20.

WKRefreshBackgroundTask 19 処理の流れ イベント発生 watchOS 起動 & タスクを渡す 完了報告 サスペンド アプリ handle(_:) でタスク毎に処理

21.

WKRefreshBackgroundTask 20 Task の種類 ・WKSnapshotRefreshBackgroundTask ・WKRelevantShortcutRefreshBackgroundTask ・WKIntentDidRunRefreshBackgroundTask ・WKURLSessionRefreshBackgroundTask ・WKWatchConnectivityRefreshBackgroundTask ・WKApplicationRefreshBackgroundTask ・WKBluetoothAlertRefreshBackgroundTask

22.

WKRefreshBackgroundTask 21 Task の種類 ・WKSnapshotRefreshBackgroundTask ・WKRelevantShortcutRefreshBackgroundTask ・WKIntentDidRunRefreshBackgroundTask ・WKURLSessionRefreshBackgroundTask ・WKWatchConnectivityRefreshBackgroundTask ・WKApplicationRefreshBackgroundTask ・WKBluetoothAlertRefreshBackgroundTask

23.

WKRefreshBackgroundTask 22 WKSnapshotRefreshBackgroundTask 以下のタイミングで、スナップショットを更新するためのタスク ・デバイスを起動した ・通知センターでアプリの通知を触った ・アプリがバックグラウンドになった ・アプリを触ってから1時間経過 ※ Snapshot は Dock や Launch Screen で表示されるもの 詳しくは Preparing to Take Your watchOS App's Snapshot を ショートカット ホーム 0° おはよう Google Maps

24.

WKRefreshBackgroundTask 23 Task の種類 ・WKSnapshotRefreshBackgroundTask ・WKRelevantShortcutRefreshBackgroundTask ・WKIntentDidRunRefreshBackgroundTask ・WKURLSessionRefreshBackgroundTask ・WKWatchConnectivityRefreshBackgroundTask ・WKApplicationRefreshBackgroundTask ・WKBluetoothAlertRefreshBackgroundTask

25.

WKRefreshBackgroundTask 24 WKRelevantShortcutRefreshBackgroundTask Siri Watch Face の Relevant Shortcut を更新するためのタスク ※ Relevant Shortcut は 詳しくは Siri Shortcuts on Siri Watch Face を WED 6 10:09 10% Happier Your daily dose is here Lose It! Log carrots and hummus Lose It! Log carrots and hummus 140 calories Confirm Dismiss Siri Shortcuts on Siri Watch Face, WWDC 18

26.

WKRefreshBackgroundTask 25 Task の種類 ・WKSnapshotRefreshBackgroundTask ・WKRelevantShortcutRefreshBackgroundTask ・WKIntentDidRunRefreshBackgroundTask ・WKURLSessionRefreshBackgroundTask ・WKWatchConnectivityRefreshBackgroundTask ・WKApplicationRefreshBackgroundTask ・WKBluetoothAlertRefreshBackgroundTask

27.

WKRefreshBackgroundTask 26 WKIntentDidRunRefreshBackgroundTask Siri Intent 経由の Shortcut 実行時に UI 等を更新するためのタスク ※ Intent は WKExtension と別プロセスで実行されるため、 Intent による変更を WKExtension でハンドルできるようにしてくれている

28.

WKRefreshBackgroundTask 27 WKIntentDidRunRefreshBackgroundTask Hey Siri, 今から入れる保険ある? ご用件は 何でしょう? ぶふっ。唐突なリクエ ストに吹き出してしま いました。 保険検索アプリ WKExtension Complication や Snapshot の更新 検索ロジック Intents App Extension

29.

WKRefreshBackgroundTask 28 Task の種類 ・WKSnapshotRefreshBackgroundTask ・WKRelevantShortcutRefreshBackgroundTask ・WKIntentDidRunRefreshBackgroundTask ・WKURLSessionRefreshBackgroundTask ・WKWatchConnectivityRefreshBackgroundTask ・WKApplicationRefreshBackgroundTask ・WKBluetoothAlertRefreshBackgroundTask

30.

WKRefreshBackgroundTask 29 WKURLSessionRefreshBackgroundTask URLSession における以下のイベントを処理するためのタスク ・認証が必要になった ・通信が完了した ※ アクティブな Watch Face にコンプリケーションがあれば 1時間に4つまで実行できる 28日(日) 21:15 Hello, World!

31.

WKRefreshBackgroundTask 30 Task の種類 ・WKSnapshotRefreshBackgroundTask ・WKRelevantShortcutRefreshBackgroundTask ・WKIntentDidRunRefreshBackgroundTask ・WKURLSessionRefreshBackgroundTask ・WKWatchConnectivityRefreshBackgroundTask ・WKApplicationRefreshBackgroundTask ・WKBluetoothAlertRefreshBackgroundTask

32.

WKRefreshBackgroundTask 31 WKWatchConnectivityRefreshBackgroundTask Watch Connectivity のうち、バックグラウンドな通信を処理するためのタスク ※ Watch Connectivity は iPhone と Apple Watch 間のデータ共有用 API で、 リアルタイムとバックグラウンドの2種類がある Interactive Messaging ・sendMessage ・sendMessageData Background Transfers ・updateApplicationContext ・transferUserInfo ・transferCurrentComplicationUserInfo ・transferFile

33.

WKRefreshBackgroundTask 32 WKWatchConnectivityRefreshBackgroundTask データ 上書き 回数/日 updateApplicationContext Dictionary される ∞ transferUserInfo Dictionary ∞ transferCurrentComplicationUserInfo Dictionary 50 transferFile URL ∞

34.

WKRefreshBackgroundTask 33 WKWatchConnectivityRefreshBackgroundTask ※ Watch Connectivity の Session は Queue に積まれ、順次処理される ※ 以下は優先度高く処理される ・sendMessage ・sendMessageData ・transferCurrentComplicationUserInfo

35.

WKRefreshBackgroundTask 34 Task の種類 ・WKSnapshotRefreshBackgroundTask ・WKRelevantShortcutRefreshBackgroundTask ・WKIntentDidRunRefreshBackgroundTask ・WKURLSessionRefreshBackgroundTask ・WKWatchConnectivityRefreshBackgroundTask ・WKApplicationRefreshBackgroundTask ・WKBluetoothAlertRefreshBackgroundTask

36.

WKRefreshBackgroundTask 35 WKApplicationRefreshBackgroundTask 任意の処理を実行するためのタスク ※ scheduleBackgroundRefresh 設定できるが、 最大1つまでで、既にある場合は上書きされる ※ アクティブな Watch Face にコンプリケーションがあれば 1時間に4つまで実行できる 28日(日) 21:15 Hello, World!

37.

WKRefreshBackgroundTask 36 WKApplicationRefreshBackgroundTask ※ Bluetooth デバイスへ接続する際は以下に注意 ・watchOS 8 ~ ・新規ペアリングはできない ・実行時に再接続できるよう、Peripheral の Advertising 間隔を短くする

38.

WKRefreshBackgroundTask 37 WKApplicationRefreshBackgroundTask ※ 設定からオフにされると詰むので祈りましょう 8:53 < 一般 App のバックグラウンド更新 App のバックグラウンド更新 App のバックグラウンド更新の機能をオフにするとバッテ リー駆動時間を節約できる場合があります。この設定で は、App に読み取りを許可したデータへのバックグラウン ドでのアクセスも制御できます。現在の文字盤にコンプリ ケーションが表示されている App は、App のバックグラウ ンド更新の設定がオフになっていても、更新が続けられま す。

39.

WKRefreshBackgroundTask 38 Task の種類 ・WKSnapshotRefreshBackgroundTask ・WKRelevantShortcutRefreshBackgroundTask ・WKIntentDidRunRefreshBackgroundTask ・WKURLSessionRefreshBackgroundTask ・WKWatchConnectivityRefreshBackgroundTask ・WKApplicationRefreshBackgroundTask ・WKBluetoothAlertRefreshBackgroundTask

40.

WKRefreshBackgroundTask 39 WKBluetoothAlertRefreshBackgroundTask Bluetooth デバイスで発生した以下のイベントを処理するためのタスク ・UUID を知っているデバイスが、接続可能状態になった ・接続中のデバイスで、特定の値が変化した 導入の背景として、 センシングした値を時間ベースではなく、閾値ベースで受け取りたい想いがあった 詳しくは Get timely alerts from Bluetooth devices on watchOS を

41.

WKRefreshBackgroundTask 40 BluetoothAlert がない時 100°C まで 温めたい どう? 90°C ヨシ! どう? 551°C アカン! 100°C 超えたで それ、 早く言ってよ〜

42.

WKRefreshBackgroundTask 41 BluetoothAlert がある時 100°C まで 温めたい どう? 90°C ヨシ! 95°Cやで アカン! 100°C 近いで

43.

WKRefreshBackgroundTask 42 WKBluetoothAlertRefreshBackgroundTask ※ 以下のデバイス要件がある ・Apple Watch が Series 6 以上 ・Bluetooth デバイスは GATT プロトコルで通信できる ※ 切断回数が多いほど、再接続可能な距離が短くなる (24h or アプリの使用でリセット) ※ デバイスのスキャンとイベントの処理は合わせて5回まで (24h or アプリの使用でリセット)

44.

WKRefreshBackgroundTask 43 Task の種類 ・WKSnapshotRefreshBackgroundTask ・WKRelevantShortcutRefreshBackgroundTask ・WKIntentDidRunRefreshBackgroundTask ・WKURLSessionRefreshBackgroundTask ・WKWatchConnectivityRefreshBackgroundTask ・WKApplicationRefreshBackgroundTask ・WKBluetoothAlertRefreshBackgroundTask

45.

WKRefreshBackgroundTask 44 実装 Tips ※ 以下の事情があるので、うまくいかないことを前提に体験設計する ・システムの状況によってはタスクがキャンセルされる ・実行されたとしても大体時間がずれる ※ 時間がかかり過ぎるとシステムに止められるので、 expireationHandler を適切に実装し、処理を引き継ぐようにする

46.

おわりに 45 watchOS におけるバックグラウンドでの実行方法 以下の3つ フォアグラウンドの作業を続ける 1. Background Session 2. Extended Runtime Session スケジュールされた作業を行う 3. WKRefreshBackgroundTask ・起点がフォアグラウンドかバックグラウンドか? ・フォアグラウンドなら Background Session で事足りるか? で使い分けていきましょう

47.

おわりに 46 参考文献 1/2 ・Enabling Background Sessions | Apple Developer Documentation ・Playing Background Audio | Apple Developer Documentation ・Running Workout Sessions | Apple Developer Documentation ・Using extended runtime sessions | Apple Developer Documentation ・Using Background Tasks | Apple Developer Documentation ・Preparing to Take Your watchOS App's Snapshot | Apple Developer Documentation ・WKSnapshotRefreshBackgroundTask | Apple Developer Documentation ・WKRelevantShortcutRefreshBackgroundTask | Apple Developer Documentation ・Siri Shortcuts on Siri Watch Face - WWDC18 ・WKIntentDidRunRefreshBackgroundTask | Apple Developer Documentation ・WKURLSessionRefreshBackgroundTask | Apple Developer Documentation

48.

おわりに 47 参考文献 2/2 ・WKWatchConnectivityRefreshBackgroundTask | Apple Developer Documentation ・Watch Connectivity | Apple Developer Documentation ・WCSession | Apple Developer Documentation ・Implementing Two-Way Communication Using Watch Connectivity | Apple Developer Documentation ・scheduleBackgroundRefresh | Apple Developer Documentation ・WKApplicationRefreshBackgroundTask | Apple Developer Documentation ・Connect Bluetooth devices to Apple Watch - WWDC21 ・Get timely alerts from Bluetooth devices on watchOS - WWDC22 ・WKBluetoothAlertRefreshBackgroundTask | Apple Developer Documentation

49.

Thank you for ing !