@@ -105,29 +105,35 @@ @implementation RNFetchBlobNetwork
105105// constructor
106106- (id )init {
107107 self = [super init ];
108- if (taskQueue == nil ) {
109- taskQueue = [[NSOperationQueue alloc ] init ];
110- taskQueue.maxConcurrentOperationCount = 10 ;
108+ @synchronized ([RNFetchBlobNetwork class ]) {
109+ if (taskQueue == nil ) {
110+ taskQueue = [[NSOperationQueue alloc ] init ];
111+ taskQueue.maxConcurrentOperationCount = 10 ;
112+ }
111113 }
112114 return self;
113115}
114116
115117+ (void ) enableProgressReport : (NSString *) taskId config : (RNFetchBlobProgress *)config
116118{
117- if (progressTable == nil )
118- {
119- 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];
120125 }
121- [progressTable setValue: config forKey: taskId];
122126}
123127
124128+ (void ) enableUploadProgress : (NSString *) taskId config : (RNFetchBlobProgress *)config
125129{
126- if (uploadProgressTable == nil )
127- {
128- 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];
129136 }
130- [uploadProgressTable setValue: config forKey: taskId];
131137}
132138
133139// removing case from headers
@@ -241,8 +247,10 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
241247 }
242248
243249 __block NSURLSessionDataTask * task = [session dataTaskWithRequest: req];
244- [taskTable setObject: task forKey: taskId];
245- [task resume ];
250+ @synchronized ([RNFetchBlobNetwork class ]){
251+ [taskTable setObject: task forKey: taskId];
252+ [task resume ];
253+ }
246254
247255 // network status indicator
248256 if ([[options objectForKey: CONFIG_INDICATOR] boolValue ] == YES )
@@ -254,21 +262,22 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
254262// #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled
255263+ (void ) emitExpiredTasks
256264{
257- NSEnumerator * emu = [expirationTable keyEnumerator ];
258- NSString * key;
259-
260- while ((key = [emu nextObject ]))
261- {
262- RCTBridge * bridge = [RNFetchBlob getRCTBridge ];
263- NSData * args = @{ @" taskId" : key };
264- [bridge.eventDispatcher sendDeviceEventWithName: EVENT_EXPIRE body: args];
265+ @synchronized ([RNFetchBlobNetwork class ]){
266+ NSEnumerator * emu = [expirationTable keyEnumerator ];
267+ NSString * key;
265268
266- }
269+ while ((key = [emu nextObject ]))
270+ {
271+ RCTBridge * bridge = [RNFetchBlob getRCTBridge ];
272+ NSData * args = @{ @" taskId" : key };
273+ [bridge.eventDispatcher sendDeviceEventWithName: EVENT_EXPIRE body: args];
267274
268- // clear expired task entries
269- [expirationTable removeAllObjects ];
270- expirationTable = [[NSMapTable alloc ] init ];
275+ }
271276
277+ // clear expired task entries
278+ [expirationTable removeAllObjects ];
279+ expirationTable = [[NSMapTable alloc ] init ];
280+ }
272281}
273282
274283// //////////////////////////////////////
@@ -448,10 +457,18 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
448457 {
449458 [writeStream write: [data bytes ] maxLength: [data length ]];
450459 }
451- RNFetchBlobProgress * pconfig = [progressTable valueForKey: taskId];
460+
452461 if (expectedBytes == 0 )
453462 return ;
463+
464+ RNFetchBlobProgress * pconfig;
465+
466+ @synchronized ([RNFetchBlobNetwork class ]){
467+ pconfig = [progressTable valueForKey: taskId];
468+ }
469+
454470 NSNumber * now =[NSNumber numberWithFloat: ((float )receivedBytes/(float )expectedBytes)];
471+
455472 if (pconfig != nil && [pconfig shouldReport: now])
456473 {
457474 [self .bridge.eventDispatcher
@@ -461,11 +478,9 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
461478 @" written" : [NSString stringWithFormat: @" %d " , receivedBytes],
462479 @" total" : [NSString stringWithFormat: @" %d " , expectedBytes],
463480 @" chunk" : chunkString
464- }
481+ }
465482 ];
466483 }
467- received = nil ;
468-
469484}
470485
471486- (void ) URLSession : (NSURLSession *)session didBecomeInvalidWithError : (nullable NSError *)error
@@ -537,7 +552,7 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
537552
538553 callback (@[ errMsg, rnfbRespType, respStr]);
539554
540- @synchronized (taskTable, uploadProgressTable, progressTable )
555+ @synchronized ([RNFetchBlobNetwork class ] )
541556 {
542557 if ([taskTable objectForKey: taskId] == nil )
543558 NSLog (@" object released by ARC." );
@@ -556,25 +571,36 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
556571// upload progress handler
557572- (void ) URLSession : (NSURLSession *)session task : (NSURLSessionTask *)task didSendBodyData : (int64_t )bytesSent totalBytesSent : (int64_t )totalBytesWritten totalBytesExpectedToSend : (int64_t )totalBytesExpectedToWrite
558573{
559- RNFetchBlobProgress * pconfig = [uploadProgressTable valueForKey: taskId];
560574 if (totalBytesExpectedToWrite == 0 )
561575 return ;
576+
577+ RNFetchBlobProgress * pconfig;
578+
579+ @synchronized ([RNFetchBlobNetwork class ]) {
580+ pconfig = [uploadProgressTable valueForKey: taskId];
581+ }
582+
562583 NSNumber * now = [NSNumber numberWithFloat: ((float )totalBytesWritten/(float )totalBytesExpectedToWrite)];
563584 if (pconfig != nil && [pconfig shouldReport: now]) {
564585 [self .bridge.eventDispatcher
565586 sendDeviceEventWithName: EVENT_PROGRESS_UPLOAD
566587 body: @{
567588 @" taskId" : taskId,
568- @" written" : [NSString stringWithFormat: @" %d " , totalBytesWritten],
569- @" total" : [NSString stringWithFormat: @" %d " , totalBytesExpectedToWrite]
589+ @" written" : [NSString stringWithFormat: @" %ld " , ( long ) totalBytesWritten],
590+ @" total" : [NSString stringWithFormat: @" %ld " , ( long ) totalBytesExpectedToWrite]
570591 }
571592 ];
572593 }
573594}
574595
575596+ (void ) cancelRequest : (NSString *)taskId
576597{
577- NSURLSessionDataTask * task = [taskTable objectForKey: taskId];
598+ NSURLSessionDataTask * task;
599+
600+ @synchronized ([RNFetchBlobNetwork class ]) {
601+ task = [taskTable objectForKey: taskId];
602+ }
603+
578604 if (task != nil && task.state == NSURLSessionTaskStateRunning )
579605 [task cancel ];
580606}
0 commit comments