applicationDidEnterBackground und applicationWillEnterForeground werden beim Drücken der Home-Taste im iOS-Simulator nicht aufgerufen
Ich brauche eine lange laufende Aufgabe, die sowohl im Hintergrund als auch im Vordergrund erledigt werden muss. Dies aktualisiert die Kerndaten. Um die Benutzeroberfläche ansprechend zu halten, habe ich einen weiteren Thread erstellt, in dem ich einen anderen managedObjectContext (MOC) verwende. Ein Timer wird also sowohl im Hintergrund als auch im Vordergrund gesetzt und bei Zustandsänderungen entsprechend deaktiviert. Bevor die Aufgabe gestartet wird und nachdem die Aufgabe abgeschlossen ist, wenn ich die Home-Taste drücke, werden die beiden Delegierungsmethoden ordnungsgemäß aufgerufen. Während die Aufgabe aktiv ist, wenn ich die Home-Taste drücke, ändert sich der Bildschirm und die Benutzeroberfläche bleibt hängen (wird leer), die beiden Delegierungsmethoden jedoch nicht richtig aufgerufen und die App wird nicht beendet. Ich konnte den Grund dafür nicht finden. Es wäre hilfreich, wenn jemand helfen kann.
Ich werde den erforderlichen Code mit diesem anfügen:
-(void) startTimerThread
{
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// Add code here to do background processing
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:[(AppDelegate *)[[UIApplication sharedApplication] delegate] persistentStoreCoordinator]];
self.managedObjectContext = context;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:context];
NSLog(@"managedObjContext : %@\n",self.managedObjectContext);
[self getDataFromFile];
dispatch_async( dispatch_get_main_queue(), ^{
// Add code here to update the UI/send notifications based on the
// results of the background processing
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadAppDelegateTable" object:nil];
[context release];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSManagedObjectContextDidSaveNotification
object:context];
});
});
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"Background\n");
[self.notificationTimer invalidate];
self.notificationTimer = nil;
UIApplication *app = [UIApplication sharedApplication];
self.bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
//start location update timer and background timer
self.timer = [NSTimer scheduledTimerWithTimeInterval:180 target:self
selector:@selector(startLocationServices) userInfo:nil repeats:YES];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
self.logDownloader.managedObjectContext = self.managedObjectContext;
NSLog(@"managedObjContext : %@\n",self.logDownloader.managedObjectContext);
self.backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:90 target:self.logDownloader selector:@selector(getDataFromFile) userInfo:nil repeats:YES];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"Foreground\n");
//invalidate background timer and location update timer
[self.timer invalidate];
[self.backgroundTimer invalidate];
self.timer = nil;
self.notificationTimer = nil;
self.logDownloader.managedObjectContext = self.managedObjectContext;
NSLog(@"managedObjContext : %@\n",self.logDownloader.managedObjectContext);
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadAppDelegateTable" object:nil];
self.notificationTimer = [NSTimer scheduledTimerWithTimeInterval:180 target:self.logDownloader selector:@selector(startTimerThread) userInfo:nil repeats:YES];
}