Browse Source

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

Ben Hsieh 7 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,11 +341,12 @@ NSMutableDictionary *fileStreams = nil;
341 341
         encoding = [encoding lowercaseString];
342 342
         if(![fm fileExistsAtPath:folder]) {
343 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 351
         NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
351 352
         NSData * content = nil;

+ 17
- 4
ios/RNFetchBlobNetwork.m View File

@@ -245,7 +245,8 @@ NSOperationQueue *taskQueue;
245 245
     }
246 246
 
247 247
     __block NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
248
-    [taskTable setObject:task forKey:taskId];
248
+    
249
+    [taskTable setObject:@{ @"session" : task, @"isCancelled" : @NO } forKey:taskId];
249 250
     [task resume];
250 251
 
251 252
     // network status indicator
@@ -503,6 +504,11 @@ NSOperationQueue *taskQueue;
503 504
     {
504 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 513
     if(respFile == YES)
508 514
     {
@@ -583,9 +589,16 @@ NSOperationQueue *taskQueue;
583 589
 
584 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