From 2c16a9488d4b883c4086ad68f61aada32c8a2095 Mon Sep 17 00:00:00 2001 From: Ben Hsieh Date: Mon, 26 Jun 2017 23:32:48 +0800 Subject: [PATCH] Fix IOS request timeout issue #368 by change default request setting to defaultSessionConfiguration, and make backgroundSessionConfigurationWithIdentifier optional --- ios/RNFetchBlobNetwork.m | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/ios/RNFetchBlobNetwork.m b/ios/RNFetchBlobNetwork.m index 85030b743..7be57fc59 100644 --- a/ios/RNFetchBlobNetwork.m +++ b/ios/RNFetchBlobNetwork.m @@ -82,6 +82,7 @@ @interface RNFetchBlobNetwork () NSMutableArray * redirects; ResponseFormat responseFormat; BOOL * followRedirect; + BOOL backgroundTask; } @end @@ -168,6 +169,8 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options self.expectedBytes = 0; self.receivedBytes = 0; self.options = options; + + backgroundTask = [options valueForKey:@"IOSBackgroundTask"] == nil ? NO : [[options valueForKey:@"IOSBackgroundTask"] boolValue]; followRedirect = [options valueForKey:@"followRedirect"] == nil ? YES : [[options valueForKey:@"followRedirect"] boolValue]; isIncrement = [options valueForKey:@"increment"] == nil ? NO : [[options valueForKey:@"increment"] boolValue]; redirects = [[NSMutableArray alloc] init]; @@ -192,13 +195,12 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options // the session trust any SSL certification NSURLSessionConfiguration *defaultConfigObject; - if(!followRedirect) - { - defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; - } - else + + defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; + + if(backgroundTask) { - NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:taskId]; + defaultConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:taskId]; } // set request timeout @@ -247,14 +249,6 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; __block UIApplication * app = [UIApplication sharedApplication]; - // #115 handling task expired when application entering backgound for a long time - UIBackgroundTaskIdentifier tid = [app beginBackgroundTaskWithName:taskId expirationHandler:^{ - NSLog([NSString stringWithFormat:@"session %@ expired", taskId ]); - [expirationTable setObject:task forKey:taskId]; - // comment out this one as it might cause app crash #271 -// [app endBackgroundTask:tid]; - }]; - } // #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled