3

数日前、NSThread に関する質問を投稿しました。最後に、毎分バックグラウンドで実行することができました。

アプリの目的は、場所を取得し、Web サービスを呼び出して、X 分ごとに ASIHTTPRequest を使用してサーバー上で更新することです。

コード:

- (void)threadEntryPoint:(ActualizarPosicionGPS2AppDelegate *)paramSender{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    while([[NSThread currentThread] isCancelled] == NO){
        [NSThread sleepForTimeInterval:60.0f];
        if ([[NSThread currentThread] isCancelled] == NO &&
            [[UIApplication sharedApplication] backgroundTimeRemaining] != DBL_MAX) {
            [self hacerCosasBackground];
        }
    }

    [pool release];
}

hacerCosasBackground は locationManager を呼び出してその位置を更新し、その位置をサーバーにアップロードします。

アプリケーション:didFinishLaunchingWithOptions:

[NSThread detachNewThreadSelector:@selector(threadEntryPoint:) toTarget:self withObject:self];


-(void) endTaskWidthIdentifier:(NSNumber *)paramIdentifier{
    UIBackgroundTaskIdentifier identifier = [paramIdentifier integerValue];
    [[UIApplication sharedApplication] endBackgroundTask:identifier];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
        NSNumber *backgroundTask = [NSNumber numberWithInteger:self.backgroundTaskIdentifier];
        [self performSelectorOnMainThread:@selector(endTaskWidthIdentifier:) withObject:backgroundTask waitUntilDone:YES];
        self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
    }];
}

ただし、NSThread は、アプリを閉じてから約 10 分後に停止します。「UIBackgroundModes」の場所を info.plist に登録しました。startMonitoringSignificantLocationChanges についても読みましたが、アプリを非常に構成可能にしたい (ユーザーは場所を更新する頻度などを設定できるようになります)、そのメソッドは 500m ごとに更新されることを理解しました (それは良くありません、私はより頻繁に場所を更新したい)。

どうすればよいですか?何か助けてください。

どうもありがとうございました!

4

0 に答える 0