-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ios): reviving PR #12411 for ios background tasks #14142
base: master
Are you sure you want to change the base?
Changes from all commits
acda1f3
7f82fba
b78daae
08b795a
b9c776e
5207393
b9246f9
29c05a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -433,6 +433,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( | |||||
// Create application support directory if not exists | ||||||
[self createDefaultDirectories]; | ||||||
|
||||||
if ([TiUtils isIOSVersionOrGreater:@"13.0"]) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove guard, not necessary |
||||||
[self registerBackgroundTasks]; | ||||||
} | ||||||
|
||||||
return YES; | ||||||
} | ||||||
|
||||||
|
@@ -1083,20 +1087,26 @@ - (void)applicationDidEnterBackground:(UIApplication *)application | |||||
|
||||||
[[NSNotificationCenter defaultCenter] postNotificationName:kTiPausedNotification object:self]; | ||||||
|
||||||
if ([TiUtils isIOSVersionOrGreater:@"13.0"]) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||||||
for (NSDictionary *backgroundTask in backgroundTasks) { | ||||||
[self submitBackgroundTask:backgroundTask]; | ||||||
} | ||||||
} | ||||||
|
||||||
if (backgroundServices == nil) { | ||||||
return; | ||||||
} | ||||||
|
||||||
UIApplication *app = [UIApplication sharedApplication]; | ||||||
TiApp *tiapp = self; | ||||||
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ | ||||||
bgTaskIdentifier = [app beginBackgroundTaskWithExpirationHandler:^{ | ||||||
// Synchronize the cleanup call on the main thread in case | ||||||
// the task actually finishes at around the same time. | ||||||
TiThreadPerformOnMainThread( | ||||||
^{ | ||||||
if (bgTask != UIBackgroundTaskInvalid) { | ||||||
[app endBackgroundTask:bgTask]; | ||||||
bgTask = UIBackgroundTaskInvalid; | ||||||
if (bgTaskIdentifier != UIBackgroundTaskInvalid) { | ||||||
[app endBackgroundTask:bgTaskIdentifier]; | ||||||
bgTaskIdentifier = UIBackgroundTaskInvalid; | ||||||
} | ||||||
}, | ||||||
NO); | ||||||
|
@@ -1202,6 +1212,8 @@ - (void)dealloc | |||||
RELEASE_TO_NIL(queuedBootEvents); | ||||||
RELEASE_TO_NIL(_queuedApplicationSelectors); | ||||||
RELEASE_TO_NIL(_applicationDelegates); | ||||||
RELEASE_TO_NIL(backgroundTasks); | ||||||
RELEASE_TO_NIL(registeredBackgroundTasks); | ||||||
|
||||||
[super dealloc]; | ||||||
} | ||||||
|
@@ -1237,6 +1249,111 @@ - (KrollBridge *)krollBridge | |||||
|
||||||
#pragma mark Background Tasks | ||||||
|
||||||
- (void)registerBackgroundTasks | ||||||
{ | ||||||
NSArray *identifiers = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"BGTaskSchedulerPermittedIdentifiers"]; | ||||||
|
||||||
for (NSString *identifier in identifiers) { | ||||||
if (registeredBackgroundTasks == nil) { | ||||||
registeredBackgroundTasks = [[NSMutableArray alloc] init]; | ||||||
} | ||||||
[[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:identifier | ||||||
usingQueue:nil | ||||||
launchHandler:^(__kindof BGTask *_Nonnull task) { | ||||||
[registeredBackgroundTasks addObject:task]; | ||||||
[self handleBGTask:task]; | ||||||
}]; | ||||||
} | ||||||
} | ||||||
|
||||||
- (void)handleBGTask:(BGTask *)task | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
NSString *notificationName = kTiBackgroundProcessNotification; | ||||||
if ([task isKindOfClass:[BGAppRefreshTask class]]) { | ||||||
// Fo refresh task submit it again | ||||||
[self submitTaskForIdentifier:task.identifier]; | ||||||
notificationName = kTiBackgroundFetchNotification; | ||||||
} | ||||||
NSString *key = [NSString stringWithFormat:@"BgTask-%@", task.identifier]; | ||||||
|
||||||
[self tryToPostBackgroundModeNotification:[NSMutableDictionary dictionaryWithObjectsAndKeys:key, @"handlerId", nil] | ||||||
withNotificationName:notificationName]; | ||||||
|
||||||
task.expirationHandler = ^{ | ||||||
if ([task isKindOfClass:[BGProcessingTask class]]) { | ||||||
// Fo processing task, if it is not completed in time then only submit it again. | ||||||
[self submitTaskForIdentifier:task.identifier]; | ||||||
} | ||||||
[task setTaskCompletedWithSuccess:false]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use proper Objc types
Suggested change
|
||||||
[registeredBackgroundTasks removeObject:task]; | ||||||
}; | ||||||
} | ||||||
|
||||||
- (void)submitTaskForIdentifier:(NSString *)identifier | ||||||
{ | ||||||
NSDictionary *backgroundTask = [self backgroundTaskForIdentifier:identifier]; | ||||||
if (backgroundTask) { | ||||||
[self submitBackgroundTask:backgroundTask]; | ||||||
} | ||||||
} | ||||||
|
||||||
- (void)submitBackgroundTask:(NSDictionary *)bgTask | ||||||
{ | ||||||
BGTaskRequest *taskRequest; | ||||||
if ([bgTask[@"type"] isEqualToString:@"process"]) { | ||||||
taskRequest = [[[BGProcessingTaskRequest alloc] initWithIdentifier:bgTask[@"identifier"]] autorelease]; | ||||||
((BGProcessingTaskRequest *)taskRequest).requiresNetworkConnectivity = [TiUtils boolValue:bgTask[@"networkConnect"] def:NO]; | ||||||
((BGProcessingTaskRequest *)taskRequest).requiresExternalPower = [TiUtils boolValue:bgTask[@"powerConnect"] def:NO]; | ||||||
} else { | ||||||
taskRequest = [[[BGAppRefreshTaskRequest alloc] initWithIdentifier:bgTask[@"identifier"]] autorelease]; | ||||||
} | ||||||
taskRequest.earliestBeginDate = bgTask[@"beginDate"]; | ||||||
|
||||||
[BGTaskScheduler.sharedScheduler submitTaskRequest:taskRequest error:nil]; | ||||||
} | ||||||
|
||||||
- (void)backgroundTaskCompletedForIdentifier:(NSString *)identifier | ||||||
{ | ||||||
for (BGTask *task in registeredBackgroundTasks) { | ||||||
if ([task.identifier isEqualToString:identifier]) { | ||||||
[task setTaskCompletedWithSuccess:YES]; | ||||||
[registeredBackgroundTasks removeObject:task]; | ||||||
break; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
- (NSDictionary *_Nullable)backgroundTaskForIdentifier:(NSString *)identifier | ||||||
{ | ||||||
NSDictionary *bgTask = nil; | ||||||
for (NSDictionary *backgroundTask in backgroundTasks) { | ||||||
if (backgroundTask[@"identifier"] == identifier) { | ||||||
bgTask = backgroundTask; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simply return here, so you save the extra assignment. Return |
||||||
break; | ||||||
} | ||||||
} | ||||||
return bgTask; | ||||||
} | ||||||
|
||||||
- (void)addBackgroundTask:(NSDictionary *)backgroundTask | ||||||
{ | ||||||
if (backgroundTasks == nil) { | ||||||
backgroundTasks = [[NSMutableArray alloc] init]; | ||||||
} | ||||||
NSArray *identifiers = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"BGTaskSchedulerPermittedIdentifiers"]; | ||||||
NSDictionary *oldTask = [self backgroundTaskForIdentifier:backgroundTask[@"identifier"]]; | ||||||
if ([identifiers containsObject:backgroundTask[@"identifier"]]) { | ||||||
if (oldTask) { | ||||||
[backgroundTasks removeObject:oldTask]; | ||||||
} | ||||||
[backgroundTasks addObject:backgroundTask]; | ||||||
} else { | ||||||
DebugLog(@"The identifier, %@, is not added in tiapp.xml. Add it against key BGTaskSchedulerPermittedIdentifiers", backgroundTask[@"identifier"]); | ||||||
} | ||||||
} | ||||||
|
||||||
#pragma mark Background Services | ||||||
|
||||||
- (void)beginBackgrounding | ||||||
{ | ||||||
if (runningServices == nil) { | ||||||
|
@@ -1383,9 +1500,9 @@ - (void)checkBackgroundServices | |||||
// the expiration handler is fired at the same time. | ||||||
TiThreadPerformOnMainThread( | ||||||
^{ | ||||||
if (bgTask != UIBackgroundTaskInvalid) { | ||||||
[[UIApplication sharedApplication] endBackgroundTask:bgTask]; | ||||||
bgTask = UIBackgroundTaskInvalid; | ||||||
if (bgTaskIdentifier != UIBackgroundTaskInvalid) { | ||||||
[[UIApplication sharedApplication] endBackgroundTask:bgTaskIdentifier]; | ||||||
bgTaskIdentifier = UIBackgroundTaskInvalid; | ||||||
} | ||||||
}, | ||||||
NO); | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2391,6 +2391,7 @@ | |
DEVELOPMENT_TEAM = ""; | ||
ENABLE_BITCODE = NO; | ||
"ENABLE_HARDENED_RUNTIME[sdk=macosx*]" = YES; | ||
EXCLUDED_ARCHS = ""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated change |
||
GCC_DYNAMIC_NO_PIC = NO; | ||
GCC_ENABLE_CPP_RTTI = NO; | ||
GCC_OPTIMIZATION_LEVEL = 0; | ||
|
@@ -2454,6 +2455,7 @@ | |
ENABLE_BITCODE = NO; | ||
"ENABLE_HARDENED_RUNTIME[sdk=macosx*]" = YES; | ||
ENABLE_NS_ASSERTIONS = NO; | ||
EXCLUDED_ARCHS = ""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated change |
||
GCC_PRECOMPILE_PREFIX_HEADER = YES; | ||
GCC_PREFIX_HEADER = Titanium_Prefix.pch; | ||
GCC_PREPROCESSOR_DEFINITIONS = ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Titanium SDK has a current minimum of iOS 13 already, so you can remove this