Skip to content

Commit 3b1bcfd

Browse files
committed
Add endBackgroundTask method
1 parent 6940038 commit 3b1bcfd

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

app/custom-app-delegate.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff 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)`);

0 commit comments

Comments
 (0)