applicationDidEnterBackground und applicationWillEnterForeground-Methode nicht aufgerufen, wenn gedrückt, home button in iOS simulator

Brauche ich eine lange laufende Aufgabe erledigt im hintergrund als auch im Vordergrund. Dies aktualisiert die Stammdaten. So pflegen UI-responsive erstellte ich ein einem anderen thread wo ich verschiedene managedObjectContext(MOC). So ein timer ist gesetzt, im hintergrund sowie im Vordergrund und inaktiviert ist angemessen, wenn sich der Zustand ändert. Bevor die Aufgabe gestartet wird, und nachdem die Aufgabe abgeschlossen ist, wenn ich drücken Sie die home-Taste ruft die beiden delegate-Methoden richtig, aber während der Auftrag aktiv ist, wenn ich drücken Sie die Taste home Bildschirm wechselt und UI hängt sich auf (leer) aber die beiden delegate-Methoden sind nicht ordnungsgemäß aufgerufen und die app wird nicht beendet. Ich konnte den Grund nicht finden, warum dies so geschieht. Es wäre hilfreich, wenn jemand helfen kann.

Werde ich befestigen Sie den benötigten code mit diesem :

-(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];
}

InformationsquelleAutor aparna | 2013-03-14

Schreibe einen Kommentar