-
-
Save girvo/b116a469231a1405800a to your computer and use it in GitHub Desktop.
Attempts at implementing background tasks in React Native iOS
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
// | |
// BackgroundTask.h | |
// tomtrack | |
// | |
// Created by Liam Edwards-Playne on 13/02/2016. | |
// | |
#import "RCTBridgeModule.h" | |
@interface BackgroundTask : NSObject <RCTBridgeModule> | |
@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
// | |
// BackgroundTask.m | |
// tomtrack | |
// | |
// Created by Liam Edwards-Playne on 13/02/2016. | |
// | |
#import <Foundation/Foundation.h> | |
#import "BackgroundTask.h" | |
#import "RCTUtils.h" | |
/* | |
Sample: | |
BackgroundTask.beginBackgroundTask("gpsTracking", () => {}) | |
*/ | |
@implementation BackgroundTask | |
RCT_EXPORT_MODULE(); | |
#pragma mark - Public API | |
RCT_EXPORT_METHOD(beginBackgroundTask:(NSString *)taskName jsCallback:(RCTResponseSenderBlock)jsCallback) | |
{ | |
UIApplication *application = RCTSharedApplication(); | |
__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithName:taskName expirationHandler:^{ | |
// Clean up any unfinished task business by marking where you | |
// stopped or ending the task outright. | |
[application endBackgroundTask:bgTask]; | |
bgTask = UIBackgroundTaskInvalid; | |
}]; | |
// Start the long-running task and return immediately. | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
printf("Running in the background\n"); | |
// Call the JS code | |
jsCallback(@[]); | |
printf("Back from the JS\n"); | |
[application endBackgroundTask:bgTask]; | |
bgTask = UIBackgroundTaskInvalid; | |
}); | |
} | |
@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
var BackgroundTask = require('react-native').NativeModules.BackgroundTask; | |
BackgroundTask.beginTask("taskName", function(){}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment