ソースを参照

Synchronized dictionaries and tables operations

Artur Chrusciel 6 年 前
コミット
17cc8b0f70
共有1 個のファイルを変更した59 個の追加37 個の削除を含む
  1. 59
    37
      ios/RNFetchBlobNetwork.m

+ 59
- 37
ios/RNFetchBlobNetwork.m ファイルの表示

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