Skip to content
This repository was archived by the owner on Feb 27, 2022. It is now read-only.

Commit 44eaa8c

Browse files
committed
Remove unnecessary @Synchronise from the code as the serial delegate queue takes care of thread safely calling the delegate methods.
1 parent b7452e8 commit 44eaa8c

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

ios/RNBackgroundDownloader.m

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ - (void)lazyInitSession {
7979
- (void)removeTaskFromMap: (NSURLSessionTask *)task {
8080
NSNumber *taskId = @(task.taskIdentifier);
8181
RNBGDTaskConfig *taskConfig = taskToConfigMap[taskId];
82-
@synchronized (taskToConfigMap) {
83-
[taskToConfigMap removeObjectForKey:taskId];
84-
[[NSUserDefaults standardUserDefaults] setObject:[self serialize: taskToConfigMap] forKey:ID_TO_CONFIG_MAP_KEY];
85-
}
82+
[taskToConfigMap removeObjectForKey:taskId];
83+
[[NSUserDefaults standardUserDefaults] setObject:[self serialize: taskToConfigMap] forKey:ID_TO_CONFIG_MAP_KEY];
8684
if (taskConfig) {
8785
[idToTaskMap removeObjectForKey:taskConfig.id];
8886
[idToPercentMap removeObjectForKey:taskConfig.id];
@@ -123,10 +121,8 @@ + (void)setCompletionHandlerWithIdentifier: (NSString *)identifier completionHan
123121

124122
NSURLSessionDownloadTask __strong *task = [urlSession downloadTaskWithRequest:request];
125123
RNBGDTaskConfig *taskConfig = [[RNBGDTaskConfig alloc] initWithDictionary: @{@"id": identifier, @"destination": destination}];
126-
@synchronized(taskToConfigMap) {
127-
taskToConfigMap[@(task.taskIdentifier)] = taskConfig;
128-
[[NSUserDefaults standardUserDefaults] setObject:[self serialize: taskToConfigMap] forKey:ID_TO_CONFIG_MAP_KEY];
129-
}
124+
taskToConfigMap[@(task.taskIdentifier)] = taskConfig;
125+
[[NSUserDefaults standardUserDefaults] setObject:[self serialize: taskToConfigMap] forKey:ID_TO_CONFIG_MAP_KEY];
130126
idToTaskMap[identifier] = task;
131127
idToPercentMap[identifier] = @0.0;
132128

@@ -216,29 +212,27 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
216212
}
217213

218214
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
219-
@synchronized (self) {
220-
RNBGDTaskConfig *taskCofig = taskToConfigMap[@(downloadTask.taskIdentifier)];
221-
if (taskCofig != nil) {
222-
if (!taskCofig.reportedBegin) {
223-
[self sendEventWithName:@"downloadBegin" body:@{@"id": taskCofig.id, @"expectedBytes": [NSNumber numberWithLongLong: totalBytesExpectedToWrite]}];
224-
taskCofig.reportedBegin = YES;
225-
}
226-
227-
NSNumber *prevPercent = idToPercentMap[taskCofig.id];
228-
NSNumber *percent = [NSNumber numberWithFloat:(float)totalBytesWritten/(float)totalBytesExpectedToWrite];
229-
if ([percent floatValue] - [prevPercent floatValue] > 0.01f) {
230-
progressReports[taskCofig.id] = @{@"id": taskCofig.id, @"written": [NSNumber numberWithLongLong: totalBytesWritten], @"total": [NSNumber numberWithLongLong: totalBytesExpectedToWrite], @"percent": percent};
231-
idToPercentMap[taskCofig.id] = percent;
232-
}
233-
234-
NSDate *now = [[NSDate alloc] init];
235-
if ([now timeIntervalSinceDate:lastProgressReport] > 1.5 && progressReports.count > 0) {
236-
if (self.bridge) {
237-
[self sendEventWithName:@"downloadProgress" body:[progressReports allValues]];
238-
}
239-
lastProgressReport = now;
240-
[progressReports removeAllObjects];
215+
RNBGDTaskConfig *taskCofig = taskToConfigMap[@(downloadTask.taskIdentifier)];
216+
if (taskCofig != nil) {
217+
if (!taskCofig.reportedBegin) {
218+
[self sendEventWithName:@"downloadBegin" body:@{@"id": taskCofig.id, @"expectedBytes": [NSNumber numberWithLongLong: totalBytesExpectedToWrite]}];
219+
taskCofig.reportedBegin = YES;
220+
}
221+
222+
NSNumber *prevPercent = idToPercentMap[taskCofig.id];
223+
NSNumber *percent = [NSNumber numberWithFloat:(float)totalBytesWritten/(float)totalBytesExpectedToWrite];
224+
if ([percent floatValue] - [prevPercent floatValue] > 0.01f) {
225+
progressReports[taskCofig.id] = @{@"id": taskCofig.id, @"written": [NSNumber numberWithLongLong: totalBytesWritten], @"total": [NSNumber numberWithLongLong: totalBytesExpectedToWrite], @"percent": percent};
226+
idToPercentMap[taskCofig.id] = percent;
227+
}
228+
229+
NSDate *now = [[NSDate alloc] init];
230+
if ([now timeIntervalSinceDate:lastProgressReport] > 1.5 && progressReports.count > 0) {
231+
if (self.bridge) {
232+
[self sendEventWithName:@"downloadProgress" body:[progressReports allValues]];
241233
}
234+
lastProgressReport = now;
235+
[progressReports removeAllObjects];
242236
}
243237
}
244238
}

0 commit comments

Comments
 (0)