File tree Expand file tree Collapse file tree 1 file changed +13
-8
lines changed
Expand file tree Collapse file tree 1 file changed +13
-8
lines changed Original file line number Diff line number Diff line change @@ -11,8 +11,7 @@ export class CustomAppDelegate extends UIResponder implements UIApplicationDeleg
1111 public applicationDidEnterBackground ( application : UIApplication ) {
1212 console . log ( "Enter background" ) ;
1313 this . bgTask = application . beginBackgroundTaskWithNameExpirationHandler ( "MyTask" , ( ) => {
14- application . endBackgroundTask ( this . bgTask ) ;
15- this . bgTask = UIBackgroundTaskInvalid ;
14+ this . endBackgroundTask ( ) ;
1615 } ) ;
1716
1817 this . timerCounter = 5 ;
@@ -24,14 +23,20 @@ export class CustomAppDelegate extends UIResponder implements UIApplicationDeleg
2423 return true ;
2524 }
2625
26+ private endBackgroundTask ( ) : void {
27+ if ( this . timer ) {
28+ this . timer . invalidate ( ) ;
29+ this . timer = null ;
30+ }
31+ this . timerCounter = 5 ;
32+ UIApplication . sharedApplication ( ) . endBackgroundTask ( this . bgTask ) ;
33+ this . bgTask = UIBackgroundTaskInvalid ;
34+ console . log ( "End of background task." ) ;
35+ }
36+
2737 public runOnBackground ( ) : void {
2838 if ( this . timerCounter <= 0 ) {
29- // end of background task
30- this . timer . invalidate ( ) ;
31- this . timerCounter = 5 ;
32- UIApplication . sharedApplication ( ) . endBackgroundTask ( this . bgTask ) ;
33- this . bgTask = UIBackgroundTaskInvalid ;
34- console . log ( "End of background task." ) ;
39+ this . endBackgroundTask ( ) ;
3540 return ;
3641 }
3742 console . log ( `${ this . timerCounter } (the app is on background)` ) ;
You can’t perform that action at this time.
0 commit comments