LoginSignup
37
31

More than 5 years have passed since last update.

BackgroundFetchの注意事項

Posted at

iOS7から追加されたバックグラウンドフェッチに関して。

バックグラウンドフェッチの間隔

基本的にバックグラウンドフェッチは開発者側で制御することができません。バックグラウンドフェッチの間隔を指定する定数がUIApplicationBackgroundFetchIntervalMinimumUIApplicationBackgroundFetchIntervalNeverのどちらかしか存在しません。

UIApplicationBackgroundFetchIntervalMinimumがシステムがサポートしている最小の間隔で、UIApplicationBackgroundFetchIntervalNeverがフェッチ処理を行わないように設定するものらしいです。そんなわけで細かな間隔を制御することができません。

BackgroundFetchを実行させるにはXcodeで設定を行い、上記のUIApplicationBackgroundFetchIntervalMinimumをアプリに対して設定する必要がおあります。


UIApplication.sharedApplication.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

あとはAppDelegateに対して以下のデリゲートメソッドを実装すればOS側が良い感じでバックグラウンドフェッチを走らせてくれるみたいです。

optional func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
- (void)application:(UIApplication *)application
performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler

注意点

completionHandler()をしっかりと呼び出すようにしましょう。以下は
- application:performFetchWithCompletionHandler:
についてのドキュメントからの引用です。

Implement this method if your app supports the fetch background mode.

アプリがバックグラウンドフェッチをサポートする場合はこのメソッドを実装すること。

When an opportunity arises to download data, the system calls this method to give your app a chance to download any data it needs. Your implementation of this method should download the data, prepare that data for use, and call the block in the completionHandler parameter.

データのダウンロードする機会が発生したとき、システムはこのメソッドを呼び出してアプリが必要なデータのダウンロードが出来るようにします。このメソッドの実装はデータのダウンロードにすべきで、使用するデータの準備をし、completionHandlerパラメータでブロックを呼び出します。

When this method is called, your app has up to 30 seconds of wall-clock time to perform the download operation and call the specified completion handler block.

このメソッドが呼ばれたとき、アプリは30秒間ダウンロードオペレーションを実行することができ、指定された完了時処理を呼び出します。

In practice, your app should call the completion handler block as soon as possible after downloading the needed data. If you do not call the completion handler in time, your app is terminated.

アプリはcompletion handlerブロックを必要なデータがダウンロードできたら可能な限り早く呼び出すようにすべきです。もしcompletion handlerを時間内に呼び出さなかった場合はアプリが終了されます。

More importantly, the system uses the elapsed time to calculate power usage and data costs for your app’s background downloads.

さらに重要なこととして、システムはアプリがバックグラウンドでのダウンロードにかかる電力使用とデータコストを計算するために経過時間を使用します。

If your app takes a long time to call the completion handler, it may be given fewer future opportunities to fetch data in the future.

もしアプリがcompletion handlerを呼び出すまでに長い時間がかかった場合、将来的にデータのフェッチにより少ない機会しか与えられないかもしれません。

まとめ

個人的には、これがすごく重要だなと思っています。

More importantly, the system uses the elapsed time to calculate power usage and data costs for your app’s background downloads.

サービスやデザインにもよりますが、なるべく少ない時間でダウンロード処理が終わるようにした方が良さそうですね。
そもそも呼び出し忘れとかは完全にNGです。。。

37
31
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
37
31