Browse Source

Synchronized dictionaries and tables operations

Artur Chrusciel 7 years ago
parent
commit
17cc8b0f70
1 changed files with 59 additions and 37 deletions
  1. 59
    37
      ios/RNFetchBlobNetwork.m

+ 59
- 37
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
-        @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
     return self;
114
     return self;
118
 
116
 
119
 + (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
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
 + (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config
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
 // removing case from headers
139
 // removing case from headers
245
     }
247
     }
246
 
248
 
247
     __block NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
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
     // network status indicator
255
     // network status indicator
252
     if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
256
     if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
258
 // #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
259
 + (void) emitExpiredTasks
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
     {
457
     {
453
         [writeStream write:[data bytes] maxLength:[data length]];
458
         [writeStream write:[data bytes] maxLength:[data length]];
454
     }
459
     }
455
-    RNFetchBlobProgress * pconfig = [progressTable valueForKey:taskId];
460
+    
456
     if(expectedBytes == 0)
461
     if(expectedBytes == 0)
457
         return;
462
         return;
463
+    
464
+    RNFetchBlobProgress * pconfig;
465
+    
466
+    @synchronized ([RNFetchBlobNetwork class]){
467
+        pconfig = [progressTable valueForKey:taskId];
468
+    }
469
+        
458
     NSNumber * now =[NSNumber numberWithFloat:((float)receivedBytes/(float)expectedBytes)];
470
     NSNumber * now =[NSNumber numberWithFloat:((float)receivedBytes/(float)expectedBytes)];
471
+    
459
     if(pconfig != nil && [pconfig shouldReport:now])
472
     if(pconfig != nil && [pconfig shouldReport:now])
460
     {
473
     {
461
         [self.bridge.eventDispatcher
474
         [self.bridge.eventDispatcher
465
                 @"written": [NSString stringWithFormat:@"%d", receivedBytes],
478
                 @"written": [NSString stringWithFormat:@"%d", receivedBytes],
466
                 @"total": [NSString stringWithFormat:@"%d", expectedBytes],
479
                 @"total": [NSString stringWithFormat:@"%d", expectedBytes],
467
                 @"chunk": chunkString
480
                 @"chunk": chunkString
468
-            }
481
+                }
469
          ];
482
          ];
470
     }
483
     }
471
-    received = nil;
472
-
473
 }
484
 }
474
 
485
 
475
 - (void) URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable NSError *)error
486
 - (void) URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable NSError *)error
541
 
552
 
542
     callback(@[ errMsg, rnfbRespType, respStr]);
553
     callback(@[ errMsg, rnfbRespType, respStr]);
543
 
554
 
544
-    @synchronized(taskTable, uploadProgressTable, progressTable)
555
+    @synchronized ([RNFetchBlobNetwork class])
545
     {
556
     {
546
         if([taskTable objectForKey:taskId] == nil)
557
         if([taskTable objectForKey:taskId] == nil)
547
             NSLog(@"object released by ARC.");
558
             NSLog(@"object released by ARC.");
560
 // upload progress handler
571
 // upload progress handler
561
 - (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
562
 {
573
 {
563
-    RNFetchBlobProgress * pconfig = [uploadProgressTable valueForKey:taskId];
564
     if(totalBytesExpectedToWrite == 0)
574
     if(totalBytesExpectedToWrite == 0)
565
         return;
575
         return;
576
+    
577
+    RNFetchBlobProgress * pconfig;
578
+    
579
+    @synchronized ([RNFetchBlobNetwork class]) {
580
+        pconfig = [uploadProgressTable valueForKey:taskId];
581
+    }
582
+    
566
     NSNumber * now = [NSNumber numberWithFloat:((float)totalBytesWritten/(float)totalBytesExpectedToWrite)];
583
     NSNumber * now = [NSNumber numberWithFloat:((float)totalBytesWritten/(float)totalBytesExpectedToWrite)];
567
     if(pconfig != nil && [pconfig shouldReport:now]) {
584
     if(pconfig != nil && [pconfig shouldReport:now]) {
568
         [self.bridge.eventDispatcher
585
         [self.bridge.eventDispatcher
569
          sendDeviceEventWithName:EVENT_PROGRESS_UPLOAD
586
          sendDeviceEventWithName:EVENT_PROGRESS_UPLOAD
570
          body:@{
587
          body:@{
571
                 @"taskId": taskId,
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
 
595
 
579
 + (void) cancelRequest:(NSString *)taskId
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
     if(task != nil && task.state == NSURLSessionTaskStateRunning)
604
     if(task != nil && task.state == NSURLSessionTaskStateRunning)
583
         [task cancel];
605
         [task cancel];
584
 }
606
 }