99#import " RCTLog.h"
1010#import < Foundation/Foundation.h>
1111#import " RCTBridge.h"
12+ #import " RNFetchBlob.h"
1213#import " RCTEventDispatcher.h"
1314#import " RNFetchBlobFS.h"
15+ #import " RCTRootView.h"
1416#import " RNFetchBlobNetwork.h"
1517#import " RNFetchBlobConst.h"
1618#import " RNFetchBlobReqBuilder.h"
2426// //////////////////////////////////////
2527
2628NSMapTable * taskTable;
29+ NSMapTable * expirationTable;
2730NSMutableDictionary * progressTable;
2831NSMutableDictionary * uploadProgressTable;
2932
@@ -37,6 +40,7 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
3740@interface RNFetchBlobNetwork ()
3841{
3942 BOOL * respFile;
43+ BOOL * isIncrement;
4044 NSString * destPath;
4145 NSOutputStream * writeStream;
4246 long bodyLength;
@@ -70,7 +74,12 @@ - (id)init {
7074 taskQueue = [[NSOperationQueue alloc ] init ];
7175 taskQueue.maxConcurrentOperationCount = 10 ;
7276 }
73- if (taskTable == nil ) {
77+ if (expirationTable == nil )
78+ {
79+ expirationTable = [[NSMapTable alloc ] init ];
80+ }
81+ if (taskTable == nil )
82+ {
7483 taskTable = [[NSMapTable alloc ] init ];
7584 }
7685 if (progressTable == nil )
@@ -133,6 +142,7 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
133142 self.expectedBytes = 0 ;
134143 self.receivedBytes = 0 ;
135144 self.options = options;
145+ isIncrement = [options valueForKey: @" increment" ] == nil ? NO : [[options valueForKey: @" increment" ] boolValue ];
136146 redirects = [[NSMutableArray alloc ] init ];
137147 [redirects addObject: req.URL.absoluteString];
138148
@@ -153,7 +163,9 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
153163 bodyLength = contentLength;
154164
155165 // the session trust any SSL certification
156- NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration ];
166+ // NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
167+ NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: taskId];
168+
157169 // set request timeout
158170 float timeout = [options valueForKey: @" timeout" ] == nil ? -1 : [[options valueForKey: @" timeout" ] floatValue ];
159171 if (timeout > 0 )
@@ -190,13 +202,39 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
190202 respData = [[NSMutableData alloc ] init ];
191203 respFile = NO ;
192204 }
193- NSURLSessionDataTask * task = [session dataTaskWithRequest: req];
205+
206+ __block NSURLSessionDataTask * task = [session dataTaskWithRequest: req];
194207 [taskTable setObject: task forKey: taskId];
195208 [task resume ];
196209
197210 // network status indicator
198211 if ([[options objectForKey: CONFIG_INDICATOR] boolValue ] == YES )
199212 [[UIApplication sharedApplication ] setNetworkActivityIndicatorVisible: YES ];
213+ __block UIApplication * app = [UIApplication sharedApplication ];
214+
215+ // #115 handling task expired when application entering backgound for a long time
216+ [app beginBackgroundTaskWithName: taskId expirationHandler: ^{
217+ NSLog ([NSString stringWithFormat: @" session %@ expired event emit" , taskId ]);
218+ [expirationTable setObject: task forKey: taskId];
219+ [app endBackgroundTask: task];
220+
221+ }];
222+
223+
224+ }
225+
226+ // #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled
227+ + (void ) getExpiredTasks
228+ {
229+ NSEnumerator * emu = [expirationTable keyEnumerator ];
230+ NSString * key;
231+
232+ while ((key = [emu nextObject ]))
233+ {
234+ RCTBridge * bridge = [RNFetchBlob getRCTBridge ];
235+ NSData * args = @{ @" taskId" : key };
236+ [bridge.eventDispatcher sendDeviceEventWithName: EVENT_EXPIRE body: args];
237+ }
200238}
201239
202240// //////////////////////////////////////
@@ -306,7 +344,7 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
306344 receivedBytes += [received longValue ];
307345 NSString * chunkString = @" " ;
308346
309- if (isInrement == YES )
347+ if (isIncrement == YES )
310348 {
311349 chunkString = [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding];
312350 }
@@ -480,6 +518,11 @@ - (void) URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthentica
480518 }
481519}
482520
521+ - (void ) URLSessionDidFinishEventsForBackgroundURLSession : (NSURLSession *)session
522+ {
523+ NSLog (@" sess done in background" );
524+ }
525+
483526- (void ) URLSession : (NSURLSession *)session task : (NSURLSessionTask *)task willPerformHTTPRedirection : (NSHTTPURLResponse *)response newRequest : (NSURLRequest *)request completionHandler : (void (^)(NSURLRequest * _Nullable))completionHandler
484527{
485528 [redirects addObject: [request.URL absoluteString ]];
0 commit comments