1

iOS7 で実行時間の長いバックグラウンド cron のようなタスクを実装しようとしています。私が今取り組んでいる方法は、10分未満の頻度で実行され、事前定義された間隔でlocationManagerのstartUpdatingLocationを開始および停止する繰り返しタイマーです。

/*
 * Very important, this function is called once a minute in the background and it specifies what          actions to take based on the number of minutes passed.
 */
-(void)everyMinuteAction
{
    NSLog(@"Every minute action");
    if ([_killTime timeIntervalSinceNow] < 0.0)
    {
        //App is dead, long live the app!
        _appKilledDueToInactivity = YES;

        //Do some stuff to indicate to the user on the UI that they should not


    }

    //Once every n minutes, we need to turn on the GPS and report our location with four points.
    NSLog(@"Modulus: %d", numberOfMinutesPassedSinceAppStarted % (int)floor((double)currentTTL/60.0));

    if ((numberOfMinutesPassedSinceAppStarted % (int)floor((double)currentTTL/60.0) == 0) || numberOfMinutesPassedSinceAppStarted == 0)
    {
        //Treat it differently if we are in foreground or background
        //GPS only responds with locations quickly when in foreground, so give it a bit more time if it is in the background.
        if (inBackground)
        {
            [NSTimer scheduledTimerWithTimeInterval:140 target:self selector:@selector(killGPSAfterCertainTime) userInfo:nil repeats:NO];
        } else {
            //Start a timer that will kill the GPS after a certain period of time, regardless of how many points it has.
        [NSTimer scheduledTimerWithTimeInterval:80 target:self selector:@selector(killGPSAfterCertainTime) userInfo:nil repeats:NO];
        }




        [self.locationManager startUpdatingLocation];

        //Control is now passed on to didUpdateLocations, which will turn off location tracking after either a set period of time or a set number of
        //locations received.

        self.timeSpentFartassingAroundTryingToGetLocations = [[NSDate alloc] init];
        self.numberOfLocationsCollectedThisTTL = 0;
    }

    if (3 % numberOfMinutesPassedSinceAppStarted == 0)
    {
        //Every 3 minutes we need to do some talking to the server
    }


    //Increment this, we've been using the app for another minute.
    numberOfMinutesPassedSinceAppStarted += 1;
}

TTL が 10 分を超える場合、これは 10 分後に強制終了されることがわかりました。また、plist ファイルでバックグラウンドの場所のアクセス許可を有効にしても機能するかどうかは完全にはわかりません。

メソッドの署名を変更し、60 秒ごとにそのサービスに登録するだけで、これを実装するために新しいネットワーク情報の取得バックグラウンド タスクを使用できるかどうか疑問に思っています。経過した分数に基づいて、ネットワークを介して何らかの情報をチェックするか、GPS で面白いビジネスを行うかを選択できました。

次の質問は、fetch API で 60 秒間隔を指定すると、実際に 60 秒ごとの更新が保証されるかどうかです。それとも、大幅なドリフトが発生しますか?

4

0 に答える 0