0

I've encountered problem with closing the app while in background. When working in the background, tapping 2x and swiping the app to close it, the app doesn't call the applicationWillTerminate:(UIApplication *)application, but goes to @autoreleasespool, which is definitely crash.

I suppose it is connected with working NSTimer, because without initializing it the app closes properly from background.

I wanted to disable the NSTimer in the appDelegate with no success, since NSTimer can be disabled only from the same thread.

I am starting NSTtimer in init of my class:

[NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(checkLastSeenTimeofPeripheral) userInfo:nil repeats:YES];

I wanted to stop it while going to background using the answer given here, but still it doesn't seem to stop the timer and the app crashes on termination.

EDIT:

Initializing in myClass

- (id)init {
    self = [super init];

    if(self){
        //check the connection timer
        [self startCheckLastSeenTimeOfPeripheralTimer];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopCheckLastSeenTimeOfPeripheralTimer) name:UIApplicationDidEnterBackgroundNotification object:[UIApplication sharedApplication]];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startCheckLastSeenTimeOfPeripheralTimer) name:UIApplicationWillEnterForegroundNotification object:[UIApplication sharedApplication]];

methods to start/stop timer

-(void)startCheckLastSeenTimeOfPeripheralTimer {
    _checkLastSeenTimeOfPeripheralTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f
                                                                           target:self
                                                                         selector:@selector(checkLastSeenTimeofPeripheral)
                                                                         userInfo:nil
                                                                          repeats:YES];
    NSLog(@"checkLastSeenTimeofPeripheralTimer started");
}

-(void)stopCheckLastSeenTimeOfPeripheralTimer {
    if (_checkLastSeenTimeOfPeripheralTimer) {
        [_checkLastSeenTimeOfPeripheralTimer invalidate];
        _checkLastSeenTimeOfPeripheralTimer = nil;
        NSLog(@"checkLastSeenTimeofPeripheralTimer stopped");
    }
    else {
        NSLog(@"checkLastSeenTimeofPeripheralTimer not initialized - can't stop");
    }
}
izik461
  • 1,131
  • 12
  • 32
  • Could you post your code? Are you using the appDelegate NSNotifications? – rcat24 Mar 11 '15 at 13:28
  • Code added. Yes, I am using NSNotifications to react when the app goes to background. – izik461 Mar 12 '15 at 08:43
  • And the stopCheckLastSeenTimeOfPeripheral is actually called, but the problem of not closing the app remains. – izik461 Mar 12 '15 at 09:11
  • Wait, the problem you're having is when the device is connected to Xcode and you're debugging? If you close your app, and you have events running then obviously the app will crash. Same will happen if you use any app and its in the middle of a networking process. Unrelatedly, make sure you remove the observers in dealloc or viewDidUnload. – rcat24 Mar 12 '15 at 09:14
  • It's happening both while debugging and normal use. In appWillTerminate I cancel all local notifications. When closing the app while in background they're never cancelled. The problem is not connected with observers since I wasn't configuring them before and the problem occured. I added them to try stopping the timer. – izik461 Mar 12 '15 at 09:41
  • You should always remove observers in the same level for when you registered them. If you registered them in the init method, you should remove it in the same class, same level which is in your case - (void)dealloc { } If the app is closed, what are you looking to achieve with the timer (other than invaliding it of course...)? – rcat24 Mar 12 '15 at 09:45
  • Ok, I remove observer in dealloc: [[NSNotificationCenter defaultCenter] removeObserver:self]; As I wrote before: I want to cancel local notifications in appWillTerminate, but it's never called when trying to close from background. Method for stopping the timer when entering background actually is called. I supposed it was the timer, because without initing it AppWillTerminate was called properly. – izik461 Mar 12 '15 at 11:12
  • Now that you are removing it in the same class, does it crash? Again, it's not really crash if you already closed your app. The only issue is if you have other pieces of code that need to be handled in the appWillTerminate method, and because you didn't remove the observer in the correct place it will never be executed. – rcat24 Mar 12 '15 at 11:24

1 Answers1

0

According to documentation appWillTerminate is not being called when closing suspended App: Suspended apps receive no notification when they are terminated; the system kills the process and reclaims the corresponding memory. Apps get suspended by the system while in background without informing about it.

izik461
  • 1,131
  • 12
  • 32