同期プロセスに 20 秒かかる場合があります (めったにありません)。バックグラウンドでセレクターを呼び出していました。同期が完了したという通知でアニメーションを停止する進行状況インジケーターがあり、以下のコードではすぐに変更されます。
[self performSelectorInBackground:@selector(updateFunction) withObject:nil];
しかし、進行を継続させるために、バックグラウンド タスクを使用することにしました。
UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
__block UIBackgroundTaskIdentifier background_task; //Create a task object
background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
[application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
//System will be shutting down the app at any point in time now
}];
//Background tasks require you to use asyncrous tasks
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Perform your tasks that your application requires
NSLog(@"\n\nRunning in the background!\n\n");
[self updateFunction];
[application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
NSLog(@"Finished in the background!");
});
しかし今では、タスクが終了すると通知が送信 (および実行) されますが、テーブル内のセルは (場合によっては) かなりの時間が経過するまで更新されません。そのため、バックグラウンド タスクなどからの「ニーズ表示」はトリガーされず、定期的な更新間隔などでのみ更新されるように思えます。
バックグラウンド タスクを使用してテーブル セルを更新する方法はありますか?
編集:通知リスナーは次のとおりです。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(refreshBttn)
name:@"finishedUpdate"
object:nil];
コードは次のとおりです。
- (void) refreshBttn {
NSLog(@"Refresh Buttun action");
iDomsAppDelegate *delegate = (iDomsAppDelegate *)[[UIApplication sharedApplication] delegate];
if([delegate updating]){
[_updateBttn setHidden:TRUE];
[_activityIndicator startAnimating];
} else {
[_updateBttn setHidden:FALSE];
[_activityIndicator stopAnimating];
}
[self setNeedsDisplay];
[self setNeedsLayout];
}