반응형
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:
Regards,
Grinarn
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:
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForegroundNotification name:UIApplicationWillEnterForegroundNotification object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willResignActiveNotification name:UIApplicationWillResignActiveNotification object:nil];
- - (void)willEnterForegroundNotification:(NSNotification *)notification {
- NSLog(@";Will Enter Foreground Notification!");
- //Start Thread
- self.locationLoader = [NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(loadLocationsWithTimer) userInfo:nil repeats:YES];
- .
- .
- .
- }
- - (void)willResignActiveNotification:(NSNotification *)botification {
- NSLog(@";Will Resign Active Notification!");
- if (self.locationLoader != nil) {
- [locationLoader invalidate];
- [locationLoader release];
- locationLoader = nil;
- }
- .
- .
- .
- }
Regards,
Grinarn
Post edited by Grinarn on
반응형
'모바일개발(Mobile Dev) > IOS개발(ObjectC)' 카테고리의 다른 글
Background Mode (0) | 2015.12.30 |
---|---|
The identity used to sign the executable is no longer valid at Xcode (0) | 2015.12.30 |
How to use UIAlertController (0) | 2015.12.27 |
storyboard id or segue (0) | 2015.12.27 |
Understanding Backgrounding (0) | 2015.12.27 |
Replies
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.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
I'm available for one-on-one help at CodeMentor
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.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
I'm available for one-on-one help at CodeMentor