Browse Source

Synchronized dictionaries and tables operations

Artur Chrusciel 6 years ago
parent
commit
38f4d75ead
1 changed files with 60 additions and 34 deletions
  1. 60
    34
      ios/RNFetchBlobNetwork.m

+ 60
- 34
ios/RNFetchBlobNetwork.m View File

105
 // constructor
105
 // constructor
106
 - (id)init {
106
 - (id)init {
107
     self = [super init];
107
     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
+        }
111
     }
113
     }
112
     return self;
114
     return self;
113
 }
115
 }
114
 
116
 
115
 + (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
117
 + (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
116
 {
118
 {
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];
120
     }
125
     }
121
-    [progressTable setValue:config forKey:taskId];
122
 }
126
 }
123
 
127
 
124
 + (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config
128
 + (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config
125
 {
129
 {
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];
129
     }
136
     }
130
-    [uploadProgressTable setValue:config forKey:taskId];
131
 }
137
 }
132
 
138
 
133
 // removing case from headers
139
 // removing case from headers
241
     }
247
     }
242
 
248
 
243
     __block NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
249
     __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
+    }
246
 
254
 
247
     // network status indicator
255
     // network status indicator
248
     if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
256
     if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
254
 // #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled
262
 // #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled
255
 + (void) emitExpiredTasks
263
 + (void) emitExpiredTasks
256
 {
264
 {
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;
265
 
268
 
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];
267
 
274
 
268
-    // clear expired task entries
269
-    [expirationTable removeAllObjects];
270
-    expirationTable = [[NSMapTable alloc] init];
275
+        }
271
 
276
 
277
+        // clear expired task entries
278
+        [expirationTable removeAllObjects];
279
+        expirationTable = [[NSMapTable alloc] init];
280
+    }
272
 }
281
 }
273
 
282
 
274
 ////////////////////////////////////////
283
 ////////////////////////////////////////
448
     {
457
     {
449
         [writeStream write:[data bytes] maxLength:[data length]];
458
         [writeStream write:[data bytes] maxLength:[data length]];
450
     }
459
     }
451
-    RNFetchBlobProgress * pconfig = [progressTable valueForKey:taskId];
460
+    
452
     if(expectedBytes == 0)
461
     if(expectedBytes == 0)
453
         return;
462
         return;
463
+    
464
+    RNFetchBlobProgress * pconfig;
465
+    
466
+    @synchronized ([RNFetchBlobNetwork class]){
467
+        pconfig = [progressTable valueForKey:taskId];
468
+    }
469
+        
454
     NSNumber * now =[NSNumber numberWithFloat:((float)receivedBytes/(float)expectedBytes)];
470
     NSNumber * now =[NSNumber numberWithFloat:((float)receivedBytes/(float)expectedBytes)];
471
+    
455
     if(pconfig != nil && [pconfig shouldReport:now])
472
     if(pconfig != nil && [pconfig shouldReport:now])
456
     {
473
     {
457
         [self.bridge.eventDispatcher
474
         [self.bridge.eventDispatcher
461
                 @"written": [NSString stringWithFormat:@"%d", receivedBytes],
478
                 @"written": [NSString stringWithFormat:@"%d", receivedBytes],
462
                 @"total": [NSString stringWithFormat:@"%d", expectedBytes],
479
                 @"total": [NSString stringWithFormat:@"%d", expectedBytes],
463
                 @"chunk": chunkString
480
                 @"chunk": chunkString
464
-            }
481
+                }
465
          ];
482
          ];
466
     }
483
     }
467
-    received = nil;
468
-
469
 }
484
 }
470
 
485
 
471
 - (void) URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable NSError *)error
486
 - (void) URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable NSError *)error
537
 
552
 
538
     callback(@[ errMsg, rnfbRespType, respStr]);
553
     callback(@[ errMsg, rnfbRespType, respStr]);
539
 
554
 
540
-    @synchronized(taskTable, uploadProgressTable, progressTable)
555
+    @synchronized ([RNFetchBlobNetwork class])
541
     {
556
     {
542
         if([taskTable objectForKey:taskId] == nil)
557
         if([taskTable objectForKey:taskId] == nil)
543
             NSLog(@"object released by ARC.");
558
             NSLog(@"object released by ARC.");
556
 // upload progress handler
571
 // upload progress handler
557
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
572
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
558
 {
573
 {
559
-    RNFetchBlobProgress * pconfig = [uploadProgressTable valueForKey:taskId];
560
     if(totalBytesExpectedToWrite == 0)
574
     if(totalBytesExpectedToWrite == 0)
561
         return;
575
         return;
576
+    
577
+    RNFetchBlobProgress * pconfig;
578
+    
579
+    @synchronized ([RNFetchBlobNetwork class]) {
580
+        pconfig = [uploadProgressTable valueForKey:taskId];
581
+    }
582
+    
562
     NSNumber * now = [NSNumber numberWithFloat:((float)totalBytesWritten/(float)totalBytesExpectedToWrite)];
583
     NSNumber * now = [NSNumber numberWithFloat:((float)totalBytesWritten/(float)totalBytesExpectedToWrite)];
563
     if(pconfig != nil && [pconfig shouldReport:now]) {
584
     if(pconfig != nil && [pconfig shouldReport:now]) {
564
         [self.bridge.eventDispatcher
585
         [self.bridge.eventDispatcher
565
          sendDeviceEventWithName:EVENT_PROGRESS_UPLOAD
586
          sendDeviceEventWithName:EVENT_PROGRESS_UPLOAD
566
          body:@{
587
          body:@{
567
                 @"taskId": taskId,
588
                 @"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]
570
                 }
591
                 }
571
          ];
592
          ];
572
     }
593
     }
574
 
595
 
575
 + (void) cancelRequest:(NSString *)taskId
596
 + (void) cancelRequest:(NSString *)taskId
576
 {
597
 {
577
-    NSURLSessionDataTask * task = [taskTable objectForKey:taskId];
598
+    NSURLSessionDataTask * task;
599
+    
600
+    @synchronized ([RNFetchBlobNetwork class]) {
601
+        task = [taskTable objectForKey:taskId];
602
+    }
603
+    
578
     if(task != nil && task.state == NSURLSessionTaskStateRunning)
604
     if(task != nil && task.state == NSURLSessionTaskStateRunning)
579
         [task cancel];
605
         [task cancel];
580
 }
606
 }