Browse Source

#268 Cancelled task should not trigger `then` promise function

Ben Hsieh 6 years ago
parent
commit
a78acc7517
2 changed files with 23 additions and 9 deletions
  1. 6
    5
      ios/RNFetchBlobFS.m
  2. 17
    4
      ios/RNFetchBlobNetwork.m

+ 6
- 5
ios/RNFetchBlobFS.m View File

341
         encoding = [encoding lowercaseString];
341
         encoding = [encoding lowercaseString];
342
         if(![fm fileExistsAtPath:folder]) {
342
         if(![fm fileExistsAtPath:folder]) {
343
             [fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:&err];
343
             [fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:&err];
344
-            [fm createFileAtPath:path contents:nil attributes:nil];
345
-        }
346
-        if(err != nil) {
347
-            reject(@"RNFetchBlob writeFile Error", @"could not create file at path", nil);
348
-            return;
344
+            if(err != nil) {
345
+                return reject(@"ENOTDIR", [NSString stringWithFormat:@"Failed to create parent directory of '%@'; error: %@", path, [err description]], nil);
346
+            }
347
+            if(![fm createFileAtPath:path contents:nil attributes:nil]) {
348
+                return reject(@"ENOENT", [NSString stringWithFormat:@"File '%@' does not exist and could not be created", path], nil);
349
+            }
349
         }
350
         }
350
         NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
351
         NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
351
         NSData * content = nil;
352
         NSData * content = nil;

+ 17
- 4
ios/RNFetchBlobNetwork.m View File

245
     }
245
     }
246
 
246
 
247
     __block NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
247
     __block NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
248
-    [taskTable setObject:task forKey:taskId];
248
+    
249
+    [taskTable setObject:@{ @"session" : task, @"isCancelled" : @NO } forKey:taskId];
249
     [task resume];
250
     [task resume];
250
 
251
 
251
     // network status indicator
252
     // network status indicator
503
     {
504
     {
504
         errMsg = [error localizedDescription];
505
         errMsg = [error localizedDescription];
505
     }
506
     }
507
+    NSDictionary * taskSession = [taskTable objectForKey:taskId];
508
+    BOOL isCancelled = [taskSession valueForKey:@"isCancelled"];
509
+    if(isCancelled) {
510
+        errMsg = @"task cancelled";
511
+    }
506
 
512
 
507
     if(respFile == YES)
513
     if(respFile == YES)
508
     {
514
     {
583
 
589
 
584
 + (void) cancelRequest:(NSString *)taskId
590
 + (void) cancelRequest:(NSString *)taskId
585
 {
591
 {
586
-    NSURLSessionDataTask * task = [taskTable objectForKey:taskId];
587
-    if(task != nil && task.state == NSURLSessionTaskStateRunning)
588
-        [task cancel];
592
+    NSDictionary * task = [taskTable objectForKey:taskId];
593
+    
594
+    if(task != nil) {
595
+        NSURLSessionDataTask * session = [task objectForKey:@"session"];
596
+        if(session.state == NSURLSessionTaskStateRunning) {
597
+            [task setValue:@NO forKey:@"isCancelled"];
598
+            [session cancel];
599
+        }
600
+    }
601
+    
589
 }
602
 }
590
 
603
 
591
 
604