Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1317 from smartdevicelink/bugfix/issue_1316_backg…
…round_task_session_creation_fails Fixed session creation failing due to no background task
- Loading branch information
Showing
6 changed files
with
142 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// SDLBackgroundTaskManager.h | ||
// SmartDeviceLink | ||
// | ||
// Created by Nicole on 6/25/19. | ||
// Copyright © 2019 smartdevicelink. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <UIKit/UIKit.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
/** | ||
* Class for managing a background task. | ||
*/ | ||
@interface SDLBackgroundTaskManager : NSObject | ||
|
||
- (instancetype)init NS_UNAVAILABLE; | ||
|
||
/** | ||
* Convenience init for starting a background task with a specific name. | ||
* | ||
* @param backgroundTaskName The name for the background task | ||
* @return A SDLBackgroundTaskManager object | ||
*/ | ||
- (instancetype)initWithBackgroundTaskName:(NSString *)backgroundTaskName; | ||
|
||
/** | ||
* Starts a background task that allows the app to establish a session while app is backgrounded. If the app is not currently backgrounded, the background task will remain dormant until the app moves to the background. | ||
*/ | ||
- (void)startBackgroundTask; | ||
|
||
/** | ||
* Cleans up a background task when it is stopped. This should be called when: | ||
* | ||
* 1. The app has established a session. | ||
* 2. The system has called the `expirationHandler` for the background task. The system may kill the app if the background task is not ended when `expirationHandler` is called. | ||
*/ | ||
- (void)endBackgroundTask; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// SDLBackgroundTaskManager.m | ||
// SmartDeviceLink | ||
// | ||
// Created by Nicole on 6/25/19. | ||
// Copyright © 2019 smartdevicelink. All rights reserved. | ||
// | ||
|
||
#import "SDLBackgroundTaskManager.h" | ||
|
||
#import "SDLLogMacros.h" | ||
|
||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface SDLBackgroundTaskManager () | ||
@property (copy, nonatomic) NSString *backgroundTaskName; | ||
@property (assign, nonatomic) UIBackgroundTaskIdentifier currentBackgroundTaskId; | ||
|
||
@end | ||
|
||
@implementation SDLBackgroundTaskManager | ||
|
||
- (instancetype)initWithBackgroundTaskName:(NSString *)backgroundTaskName { | ||
SDLLogV(@"SDLBackgroundTaskManager init with name %@", backgroundTaskName); | ||
self = [super init]; | ||
if (!self) { | ||
return nil; | ||
} | ||
|
||
_backgroundTaskName = backgroundTaskName; | ||
|
||
return self; | ||
} | ||
|
||
- (void)startBackgroundTask { | ||
if (self.currentBackgroundTaskId != UIBackgroundTaskInvalid) { | ||
SDLLogV(@"The %@ background task is already running.", self.backgroundTaskName); | ||
return; | ||
} | ||
|
||
__weak typeof(self) weakself = self; | ||
self.currentBackgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithName:self.backgroundTaskName expirationHandler:^{ | ||
SDLLogD(@"The %@ background task expired", self.backgroundTaskName); | ||
[weakself endBackgroundTask]; | ||
}]; | ||
|
||
SDLLogD(@"The %@ background task started with id: %lu", self.backgroundTaskName, (unsigned long)self.currentBackgroundTaskId); | ||
} | ||
|
||
- (void)endBackgroundTask { | ||
if (self.currentBackgroundTaskId == UIBackgroundTaskInvalid) { | ||
SDLLogV(@"Background task already ended. Returning..."); | ||
return; | ||
} | ||
|
||
SDLLogD(@"Ending background task with id: %lu", (unsigned long)self.currentBackgroundTaskId); | ||
[[UIApplication sharedApplication] endBackgroundTask:self.currentBackgroundTaskId]; | ||
self.currentBackgroundTaskId = UIBackgroundTaskInvalid; | ||
} | ||
|
||
- (void)dealloc { | ||
[self endBackgroundTask]; | ||
} | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.