beginBackgroundTaskWithExpirationHandlerに関するいくつかの調査を行いましたが、正しく理解しているとは思いません。私は、私たちのアプリがバックグラウンドになったときに長いタスクをラップすることができるという印象を受けており、そのタスクは引き続き実行されます。最初にアプリケーションを実行すると、より多くの情報をユーザに提供する、より多くのデータをサーバからダウンロードできます。これにはしばらく時間がかかりますので、このタスクをバックグラウンドモードとスリープモード(可能な場合)で実行したいと考えています。beginBackgroundTaskWithExpirationHandler、サーバーからデータをダウンロードしています
-(void) HandleDownload:(NSNotification *)notification
{
DataManager *dmgr = [TestApp sharedDelegate].AppDataManager;
NSString *nextPacketName = [dmgr GetNextPacketName];
int totalNumberPackets = [dmgr GetRowCountForTable:@"Manifest"];
if (nextPacketName == nil)
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:DownloadReferenceSequenceNotification object:nil];
self.ProgressLabel.hidden = YES;
self.ProgressBar.hidden = YES;
}
else
{
self.ProgressLabel.hidden = NO;
self.ProgressLabel.text = [NSString stringWithFormat:@"Downloading %@", nextPacketName];
self.ProgressBar.hidden = NO;
self.ProgressBar.progress += 1./(totalNumberPackets);
WebServiceManager *wmgr = [WebServiceManager sharedInstance];
[wmgr GetPacket:nextPacketName]; // This function creates the NSURL and starts the request
}
}
この機能は、アプリがフォアグラウンドであると仮定すると動作します。ユーザーは、ダウンロードをクリックしたときに呼び出されるbeginBackgroundTaskWithExpirationHandlerなし
私の本来の機能は次のようになります。私は、このタスクをバックグラウンドでも持ってやってみましたことはある:私はこれについてbeginBackgroundTaskWithExpirationHandlerブロックに私の長い実行中のタスクをラップしようとしている中に正しい方法を
-(void) HandleDownload:(NSNotification *)notification
{
UIApplication *app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier taskID;
taskID = [app beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"Background task not completed");
[app endBackgroundTask:taskID];
DataManager *dmgr = [TestApp sharedDelegate].AppDataManager;
NSString *nextPacketName = [dmgr GetNextPacketName];
int totalNumberPackets = [dmgr GetRowCountForTable:@"Manifest"];
if (nextPacketName == nil)
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:DownloadReferenceSequenceNotification object:nil];
self.ProgressLabel.hidden = YES;
self.ProgressBar.hidden = YES;
}
else
{
self.ProgressLabel.hidden = NO;
self.ProgressLabel.text = [NSString stringWithFormat:@"Downloading %@", nextPacketName];
self.ProgressBar.hidden = NO;
self.ProgressBar.progress += 1./(totalNumberPackets);
WebServiceManager *wmgr = [WebServiceManager sharedInstance];
[wmgr GetPacket:nextPacketName]; // This function creates the NSURL and starts the request
}
NSLog(@"Time remaining: %f", app.backgroundTimeRemaining);
}];
if (taskID == UIBackgroundTaskInvalid) {
NSLog(@"Failed to create background task identifier");
}
}
行くのだろうか?私はexpiratinHandlerブロック内のどのコードも呼び出されていないので、私のようには見えません。前もって感謝します!