Ver código fonte

Synchronized dictionaries and tables operations

Artur Chrusciel 6 anos atrás
pai
commit
38f4d75ead
1 arquivos alterados com 60 adições e 34 exclusões
  1. 60
    34
      ios/RNFetchBlobNetwork.m

+ 60
- 34
ios/RNFetchBlobNetwork.m Ver arquivo

@@ -105,29 +105,35 @@ NSOperationQueue *taskQueue;
105 105
 // constructor
106 106
 - (id)init {
107 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 114
     return self;
113 115
 }
114 116
 
115 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 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 139
 // removing case from headers
@@ -241,8 +247,10 @@ NSOperationQueue *taskQueue;
241 247
     }
242 248
 
243 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 255
     // network status indicator
248 256
     if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
@@ -254,21 +262,22 @@ NSOperationQueue *taskQueue;
254 262
 // #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled
255 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,10 +457,18 @@ NSOperationQueue *taskQueue;
448 457
     {
449 458
         [writeStream write:[data bytes] maxLength:[data length]];
450 459
     }
451
-    RNFetchBlobProgress * pconfig = [progressTable valueForKey:taskId];
460
+    
452 461
     if(expectedBytes == 0)
453 462
         return;
463
+    
464
+    RNFetchBlobProgress * pconfig;
465
+    
466
+    @synchronized ([RNFetchBlobNetwork class]){
467
+        pconfig = [progressTable valueForKey:taskId];
468
+    }
469
+        
454 470
     NSNumber * now =[NSNumber numberWithFloat:((float)receivedBytes/(float)expectedBytes)];
471
+    
455 472
     if(pconfig != nil && [pconfig shouldReport:now])
456 473
     {
457 474
         [self.bridge.eventDispatcher
@@ -461,11 +478,9 @@ NSOperationQueue *taskQueue;
461 478
                 @"written": [NSString stringWithFormat:@"%d", receivedBytes],
462 479
                 @"total": [NSString stringWithFormat:@"%d", expectedBytes],
463 480
                 @"chunk": chunkString
464
-            }
481
+                }
465 482
          ];
466 483
     }
467
-    received = nil;
468
-
469 484
 }
470 485
 
471 486
 - (void) URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable NSError *)error
@@ -537,7 +552,7 @@ NSOperationQueue *taskQueue;
537 552
 
538 553
     callback(@[ errMsg, rnfbRespType, respStr]);
539 554
 
540
-    @synchronized(taskTable, uploadProgressTable, progressTable)
555
+    @synchronized ([RNFetchBlobNetwork class])
541 556
     {
542 557
         if([taskTable objectForKey:taskId] == nil)
543 558
             NSLog(@"object released by ARC.");
@@ -556,17 +571,23 @@ NSOperationQueue *taskQueue;
556 571
 // upload progress handler
557 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 574
     if(totalBytesExpectedToWrite == 0)
561 575
         return;
576
+    
577
+    RNFetchBlobProgress * pconfig;
578
+    
579
+    @synchronized ([RNFetchBlobNetwork class]) {
580
+        pconfig = [uploadProgressTable valueForKey:taskId];
581
+    }
582
+    
562 583
     NSNumber * now = [NSNumber numberWithFloat:((float)totalBytesWritten/(float)totalBytesExpectedToWrite)];
563 584
     if(pconfig != nil && [pconfig shouldReport:now]) {
564 585
         [self.bridge.eventDispatcher
565 586
          sendDeviceEventWithName:EVENT_PROGRESS_UPLOAD
566 587
          body:@{
567 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,7 +595,12 @@ NSOperationQueue *taskQueue;
574 595
 
575 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 604
     if(task != nil && task.state == NSURLSessionTaskStateRunning)
579 605
         [task cancel];
580 606
 }