Skip to content

Commit a78acc7

Browse files
committed
#268 Cancelled task should not trigger then promise function
1 parent c6f8b5d commit a78acc7

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

ios/RNFetchBlobFS.m

+6-5
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,12 @@ + (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString
341341
encoding = [encoding lowercaseString];
342342
if(![fm fileExistsAtPath:folder]) {
343343
[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+
}
349350
}
350351
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
351352
NSData * content = nil;

ios/RNFetchBlobNetwork.m

+17-4
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
245245
}
246246

247247
__block NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
248-
[taskTable setObject:task forKey:taskId];
248+
249+
[taskTable setObject:@{ @"session" : task, @"isCancelled" : @NO } forKey:taskId];
249250
[task resume];
250251

251252
// network status indicator
@@ -503,6 +504,11 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
503504
{
504505
errMsg = [error localizedDescription];
505506
}
507+
NSDictionary * taskSession = [taskTable objectForKey:taskId];
508+
BOOL isCancelled = [taskSession valueForKey:@"isCancelled"];
509+
if(isCancelled) {
510+
errMsg = @"task cancelled";
511+
}
506512

507513
if(respFile == YES)
508514
{
@@ -583,9 +589,16 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSen
583589

584590
+ (void) cancelRequest:(NSString *)taskId
585591
{
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+
589602
}
590603

591604

0 commit comments

Comments
 (0)