본문 바로가기
모바일개발(Mobile Dev)/IOS개발(ObjectC)

UIApplicationDidEnterBackgroundNotification UIApplicationWillEnterForegroundNotification

by 테크한스 2015. 12. 27.
반응형
When the app becomes active again, i listen to the willEnterForegroundNotification and then i create a new timer object, which starts to refresh my data.

Works fine most of the time, but sometimes it seems, that the willEnterForegroundNotification does not get send and so my new timer never gets started. But its hard for me to test that, because it works 95% of the time.

Have you guys ever had such a problem with the notifications? Heres some Code:
  1. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForegroundNotification:) name:UIApplicationWillEnterForegroundNotification object:nil];
  2. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willResignActiveNotification:) name:UIApplicationWillResignActiveNotification object:nil];
  1. - (void)willEnterForegroundNotification:(NSNotification *)notification {
  2. NSLog(@";Will Enter Foreground Notification!");
  3. //Start Thread
  4. self.locationLoader = [NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(loadLocationsWithTimer) userInfo:nil repeats:YES];
  5. .
  6. .
  7. .
  8. }
  1. - (void)willResignActiveNotification:(NSNotification *)botification {
  2. NSLog(@";Will Resign Active Notification!");
  3. if (self.locationLoader != nil) {
  4. [locationLoader invalidate];
  5. [locationLoader release];
  6. locationLoader = nil;
  7. }
  8. .
  9. .
  10. .
  11. }

Regards,
Grinarn
Post edited by Grinarn on 
 

Replies

  • Duncan CDuncan C Posts: 9,098Tutorial Authors, Registered Users @ @ @ @ @ @ @
     edited July 2011
    Grinarn wrote: »
    Hi there,

    i have a viewcontroller where a timer refreshes the data every 15 sec. Wenn the app gets inactive, i listen for the Notification and then i invalidate my timer, so the the data does not use to much battery power in the background.

    When the app becomes active again, i listen to the willEnterForegroundNotification and then i create a new timer object, which starts to refresh my data.

    Works fine most of the time, but sometimes it seems, that the willEnterForegroundNotification does not get send and so my new timer never gets started. But its hard for me to test that, because it works 95% of the time.

    Have you guys ever had such a problem with the notifications? Heres some Code:
    1. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForegroundNotification:) name:UIApplicationWillEnterForegroundNotification object:nil];
    2. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willResignActiveNotification:) name:UIApplicationWillResignActiveNotification object:nil];
    1. - (void)willEnterForegroundNotification:(NSNotification *)notification {
    2. NSLog(@";Will Enter Foreground Notification!");
    3. //Start Thread
    4. self.locationLoader = [NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(loadLocationsWithTimer) userInfo:nil repeats:YES];
    5. .
    6. .
    7. .
    8. }
    1. - (void)willResignActiveNotification:(NSNotification *)botification {
    2. NSLog(@";Will Resign Active Notification!");
    3. if (self.locationLoader != nil) {
    4. [locationLoader invalidate];
    5. [locationLoader release];
    6. locationLoader = nil;
    7. }
    8. .
    9. .
    10. .
    11. }

    Regards,
    Grinarn

    I think I know what the problem is.

    UIApplicationWillResignActiveNotification and UIApplicationWillEnterForegroundNotification are not the matching notifications.

    You want the UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification notifications. The UIApplicationWillResignActiveNotification notification gets sent for things like displaying a text message or other notification. In that case, your app loses focus, but does not enter the background. Your app only enters the background if the user then taps on the notification and launches the messages app.

    Your current code works most of the time because usually, a UIApplicationWillResignActiveNotification is followed by a UIApplicationDidEnterBackgroundNotification, but not always.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
     
  • GrinarnGrinarn Posts: 180Registered Users @ @
     edited July 2011
    Duncan C wrote: »
    I think I know what the problem is.

    UIApplicationWillResignActiveNotification and UIApplicationWillEnterForegroundNotification are not the matching notifications.

    You want the UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification notifications. The UIApplicationWillResignActiveNotification notification gets sent for things like displaying a text message or other notification. In that case, your app loses focus, but does not enter the background. Your app only enters the background if the user then taps on the notification and launches the messages app.

    Your current code works most of the time because usually, a UIApplicationWillResignActiveNotification is followed by a UIApplicationDidEnterBackgroundNotification, but not always.

    Shouldnt it be UIApplicationDidBecomeActiveNotification and UIApplicationDidEnterBackgroundNotification then?
     
  • Duncan CDuncan C Posts: 9,098Tutorial Authors, Registered Users @ @ @ @ @ @ @
     edited July 2011
    Grinarn wrote: »
    Shouldnt it be UIApplicationDidBecomeActiveNotification and UIApplicationDidEnterBackgroundNotification then?

    No.

    Active/inactive is a different state than foreground/background. You can become inactive without entering the background. That's why you are having problems.

    Write your code to respond to UIApplicationDidEnterBackgroundNotification to handle a transition to background (invalidating the timer)

    and UIApplicationWillEnterForegroundNotification to handle returning to foreground.

    Think about it. Those notifications are opposites of each other. Once tells you you are entering the foreground, and the other tells you you have entered the background.

    UIApplicationDidBecomeActiveNotification and UIApplicationDidEnterBackgroundNotification are not opposites. That's why you are getting yourself in trouble.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
    · 
  • GrinarnGrinarn Posts: 180Registered Users @ @
     edited July 2011
    Thanks for clearing that. Seems to work better now.


반응형