Skip to content

Commit 17cc8b0

Browse files
author
Artur Chrusciel
committed
Synchronized dictionaries and tables operations
1 parent e46f94e commit 17cc8b0

File tree

1 file changed

+59
-37
lines changed

1 file changed

+59
-37
lines changed

ios/RNFetchBlobNetwork.m

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -105,33 +105,35 @@ @implementation RNFetchBlobNetwork
105105
// constructor
106106
- (id)init {
107107
self = [super init];
108-
if(taskQueue == nil) {
109-
@synchronized ([RNFetchBlobNetwork class]) {
110-
if (taskQueue == nil) {
111-
taskQueue = [[NSOperationQueue alloc] init];
112-
taskQueue.maxConcurrentOperationCount = 10;
113-
}
108+
@synchronized ([RNFetchBlobNetwork class]) {
109+
if (taskQueue == nil) {
110+
taskQueue = [[NSOperationQueue alloc] init];
111+
taskQueue.maxConcurrentOperationCount = 10;
114112
}
115113
}
116114
return self;
117115
}
118116

119117
+ (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
120118
{
121-
if(progressTable == nil)
122-
{
123-
progressTable = [[NSMutableDictionary alloc] init];
119+
@synchronized ([RNFetchBlobNetwork class]) {
120+
if(progressTable == nil)
121+
{
122+
progressTable = [[NSMutableDictionary alloc] init];
123+
}
124+
[progressTable setValue:config forKey:taskId];
124125
}
125-
[progressTable setValue:config forKey:taskId];
126126
}
127127

128128
+ (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config
129129
{
130-
if(uploadProgressTable == nil)
131-
{
132-
uploadProgressTable = [[NSMutableDictionary alloc] init];
130+
@synchronized ([RNFetchBlobNetwork class]) {
131+
if(uploadProgressTable == nil)
132+
{
133+
uploadProgressTable = [[NSMutableDictionary alloc] init];
134+
}
135+
[uploadProgressTable setValue:config forKey:taskId];
133136
}
134-
[uploadProgressTable setValue:config forKey:taskId];
135137
}
136138

137139
// removing case from headers
@@ -245,8 +247,10 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
245247
}
246248

247249
__block NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
248-
[taskTable setObject:task forKey:taskId];
249-
[task resume];
250+
@synchronized ([RNFetchBlobNetwork class]){
251+
[taskTable setObject:task forKey:taskId];
252+
[task resume];
253+
}
250254

251255
// network status indicator
252256
if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
@@ -258,21 +262,22 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
258262
// #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled
259263
+ (void) emitExpiredTasks
260264
{
261-
NSEnumerator * emu = [expirationTable keyEnumerator];
262-
NSString * key;
263-
264-
while((key = [emu nextObject]))
265-
{
266-
RCTBridge * bridge = [RNFetchBlob getRCTBridge];
267-
NSData * args = @{ @"taskId": key };
268-
[bridge.eventDispatcher sendDeviceEventWithName:EVENT_EXPIRE body:args];
265+
@synchronized ([RNFetchBlobNetwork class]){
266+
NSEnumerator * emu = [expirationTable keyEnumerator];
267+
NSString * key;
269268

270-
}
269+
while((key = [emu nextObject]))
270+
{
271+
RCTBridge * bridge = [RNFetchBlob getRCTBridge];
272+
NSData * args = @{ @"taskId": key };
273+
[bridge.eventDispatcher sendDeviceEventWithName:EVENT_EXPIRE body:args];
271274

272-
// clear expired task entries
273-
[expirationTable removeAllObjects];
274-
expirationTable = [[NSMapTable alloc] init];
275+
}
275276

277+
// clear expired task entries
278+
[expirationTable removeAllObjects];
279+
expirationTable = [[NSMapTable alloc] init];
280+
}
276281
}
277282

278283
////////////////////////////////////////
@@ -452,10 +457,18 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
452457
{
453458
[writeStream write:[data bytes] maxLength:[data length]];
454459
}
455-
RNFetchBlobProgress * pconfig = [progressTable valueForKey:taskId];
460+
456461
if(expectedBytes == 0)
457462
return;
463+
464+
RNFetchBlobProgress * pconfig;
465+
466+
@synchronized ([RNFetchBlobNetwork class]){
467+
pconfig = [progressTable valueForKey:taskId];
468+
}
469+
458470
NSNumber * now =[NSNumber numberWithFloat:((float)receivedBytes/(float)expectedBytes)];
471+
459472
if(pconfig != nil && [pconfig shouldReport:now])
460473
{
461474
[self.bridge.eventDispatcher
@@ -465,11 +478,9 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
465478
@"written": [NSString stringWithFormat:@"%d", receivedBytes],
466479
@"total": [NSString stringWithFormat:@"%d", expectedBytes],
467480
@"chunk": chunkString
468-
}
481+
}
469482
];
470483
}
471-
received = nil;
472-
473484
}
474485

475486
- (void) URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable NSError *)error
@@ -541,7 +552,7 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
541552

542553
callback(@[ errMsg, rnfbRespType, respStr]);
543554

544-
@synchronized(taskTable, uploadProgressTable, progressTable)
555+
@synchronized ([RNFetchBlobNetwork class])
545556
{
546557
if([taskTable objectForKey:taskId] == nil)
547558
NSLog(@"object released by ARC.");
@@ -560,25 +571,36 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
560571
// upload progress handler
561572
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
562573
{
563-
RNFetchBlobProgress * pconfig = [uploadProgressTable valueForKey:taskId];
564574
if(totalBytesExpectedToWrite == 0)
565575
return;
576+
577+
RNFetchBlobProgress * pconfig;
578+
579+
@synchronized ([RNFetchBlobNetwork class]) {
580+
pconfig = [uploadProgressTable valueForKey:taskId];
581+
}
582+
566583
NSNumber * now = [NSNumber numberWithFloat:((float)totalBytesWritten/(float)totalBytesExpectedToWrite)];
567584
if(pconfig != nil && [pconfig shouldReport:now]) {
568585
[self.bridge.eventDispatcher
569586
sendDeviceEventWithName:EVENT_PROGRESS_UPLOAD
570587
body:@{
571588
@"taskId": taskId,
572-
@"written": [NSString stringWithFormat:@"%d", totalBytesWritten],
573-
@"total": [NSString stringWithFormat:@"%d", totalBytesExpectedToWrite]
589+
@"written": [NSString stringWithFormat:@"%ld", (long) totalBytesWritten],
590+
@"total": [NSString stringWithFormat:@"%ld", (long) totalBytesExpectedToWrite]
574591
}
575592
];
576593
}
577594
}
578595

579596
+ (void) cancelRequest:(NSString *)taskId
580597
{
581-
NSURLSessionDataTask * task = [taskTable objectForKey:taskId];
598+
NSURLSessionDataTask * task;
599+
600+
@synchronized ([RNFetchBlobNetwork class]) {
601+
task = [taskTable objectForKey:taskId];
602+
}
603+
582604
if(task != nil && task.state == NSURLSessionTaskStateRunning)
583605
[task cancel];
584606
}

0 commit comments

Comments
 (0)