9
9
#import " RNBackgroundDownloader.h"
10
10
#import " RNBGDTaskConfig.h"
11
11
12
- #define URL_TO_CONFIG_MAP_KEY @" com.eko.bgdownloadmap "
12
+ #define ID_TO_CONFIG_MAP_KEY @" com.eko.bgdownloadidmap "
13
13
14
14
static CompletionHandler storedCompletionHandler;
15
15
16
16
@implementation RNBackgroundDownloader {
17
17
NSURLSession *urlSession;
18
18
NSURLSessionConfiguration *sessionConfig;
19
- NSMutableDictionary <NSString *, RNBGDTaskConfig *> *urlToConfigMap;
20
19
NSMutableDictionary <NSNumber *, RNBGDTaskConfig *> *taskToConfigMap;
21
20
NSMutableDictionary <NSString *, NSURLSessionDownloadTask *> *idToTaskMap;
22
21
NSMutableDictionary <NSString *, NSData *> *idToResumeDataMap;
@@ -55,11 +54,10 @@ - (NSDictionary *)constantsToExport {
55
54
- (id ) init {
56
55
self = [super init ];
57
56
if (self) {
58
- urlToConfigMap = [self deserialize: [[NSUserDefaults standardUserDefaults ] objectForKey: URL_TO_CONFIG_MAP_KEY ]];
59
- if (urlToConfigMap == nil ) {
60
- urlToConfigMap = [[NSMutableDictionary alloc ] init ];
57
+ taskToConfigMap = [self deserialize: [[NSUserDefaults standardUserDefaults ] objectForKey: ID_TO_CONFIG_MAP_KEY ]];
58
+ if (taskToConfigMap == nil ) {
59
+ taskToConfigMap = [[NSMutableDictionary alloc ] init ];
61
60
}
62
- taskToConfigMap = [[NSMutableDictionary alloc ] init ];
63
61
idToTaskMap = [[NSMutableDictionary alloc ] init ];
64
62
idToResumeDataMap= [[NSMutableDictionary alloc ] init ];
65
63
idToPercentMap = [[NSMutableDictionary alloc ] init ];
@@ -81,10 +79,9 @@ - (void)lazyInitSession {
81
79
- (void )removeTaskFromMap : (NSURLSessionTask *)task {
82
80
NSNumber *taskId = @(task.taskIdentifier );
83
81
RNBGDTaskConfig *taskConfig = taskToConfigMap[taskId];
84
- [taskToConfigMap removeObjectForKey: taskId];
85
- @synchronized (urlToConfigMap) {
86
- [urlToConfigMap removeObjectForKey: task.currentRequest.URL.absoluteString];
87
- [[NSUserDefaults standardUserDefaults ] setObject: [self serialize: urlToConfigMap] forKey: URL_TO_CONFIG_MAP_KEY];
82
+ @synchronized (taskToConfigMap) {
83
+ [taskToConfigMap removeObjectForKey: taskId];
84
+ [[NSUserDefaults standardUserDefaults ] setObject: [self serialize: taskToConfigMap] forKey: ID_TO_CONFIG_MAP_KEY];
88
85
}
89
86
if (taskConfig) {
90
87
[idToTaskMap removeObjectForKey: taskConfig.id ];
@@ -126,7 +123,10 @@ + (void)setCompletionHandlerWithIdentifier: (NSString *)identifier completionHan
126
123
127
124
NSURLSessionDownloadTask *task = [urlSession downloadTaskWithRequest: request];
128
125
RNBGDTaskConfig *taskConfig = [[RNBGDTaskConfig alloc ] initWithDictionary: @{@" id" : identifier, @" destination" : destination}];
129
- taskToConfigMap[@(task.taskIdentifier)] = taskConfig;
126
+ @synchronized (taskToConfigMap) {
127
+ taskToConfigMap[@(task.taskIdentifier)] = taskConfig;
128
+ [[NSUserDefaults standardUserDefaults ] setObject: [self serialize: taskToConfigMap] forKey: ID_TO_CONFIG_MAP_KEY];
129
+ }
130
130
idToTaskMap[identifier] = task;
131
131
idToPercentMap[identifier] = @0.0 ;
132
132
@@ -161,7 +161,7 @@ + (void)setCompletionHandlerWithIdentifier: (NSString *)identifier completionHan
161
161
NSMutableArray *idsFound = [[NSMutableArray alloc ] init ];
162
162
for (NSURLSessionDownloadTask *foundTask in downloadTasks) {
163
163
NSURLSessionDownloadTask __strong *task = foundTask;
164
- RNBGDTaskConfig *taskConfig = urlToConfigMap[ task.currentRequest.URL.absoluteString ];
164
+ RNBGDTaskConfig *taskConfig = taskToConfigMap[@( task.taskIdentifier) ];
165
165
if (taskConfig) {
166
166
if (task.state == NSURLSessionTaskStateCompleted && task.countOfBytesReceived < task.countOfBytesExpectedToReceive ) {
167
167
if (task.error && task.error .code == -999 && task.error .userInfo [NSURLSessionDownloadTaskResumeData ] != nil ) {
@@ -221,8 +221,6 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
221
221
if (taskCofig != nil ) {
222
222
if (!taskCofig.reportedBegin ) {
223
223
[self sendEventWithName: @" downloadBegin" body: @{@" id" : taskCofig.id , @" expectedBytes" : [NSNumber numberWithLongLong: totalBytesExpectedToWrite]}];
224
- urlToConfigMap[downloadTask.currentRequest.URL.absoluteString] = taskCofig;
225
- [[NSUserDefaults standardUserDefaults ] setObject: [self serialize: urlToConfigMap] forKey: URL_TO_CONFIG_MAP_KEY];
226
224
taskCofig.reportedBegin = YES ;
227
225
}
228
226
0 commit comments