react-native-fetch-blob
react-native-fetch-blob copied to clipboard
iOS: Lost connection to background transfer service
Hi, I trying to do a background download on iOS. this is my config:
let task = RNFB
.config({
overwrite: !headers,
path: tempPath,
IOSBackgroundTask: true,
indicator: true
})
.fetch('GET', url, headers);
When going to the background and back I get an exception: Lost connection to background transfer service. I looked everywhere but found no solution.
RNFB v: 0.10.6 RN v: 0.45.1
Thank you.
I'm experiencing the same issue. After setting IOSBackgroundTask to true the task stops uploading upon going into the background and I get the same error.
Same for me!
+1 when the system kill the app, I think it would better to allow app keep downloading
Check out the Apple documentation here.
Modifying the dataTaskWithRequest
in RNFetchBlobNetwork.m
to uploadTaskWithStreamedRequest
or downloadTaskWithRequest
will resolve the issue.
I don't have time to make a full PR, but you can see how you might pass in the HTTP method down to sendRequest
to build the appropriate task.
@danielsuo I have updated the files for downloadTaskWithRequest and uploadTaskWithStreamedRequest support. Below is the implementation logic
Upload Task : export function uploadImage(postUrl, fileData) { RNFetchBlob.config({ IOSBackgroundTask : true, IOSUploadTask : true, uploadFilePath : fileData.uri }).fetch('POST', postUrl, { 'Content-Type': 'application/octet-stream', }).then((res) => { /*if (responseJson.code != 200) { Alert.alert(responseJson.message) } */
return responseJson;
}).catch((err) => {
console.log('err', err)
})
}
Download task :
export function savingDataInCache(value) {
value.map((object, pos) => {
let urlVal = ${api.BASE_URL}/api/v1/download/fs/${object.output_location}/${object.user_document_name}
;
RNFetchBlob.config({
IOSBackgroundTask : true,
IOSDownloadTask : true,
path: getCachePath() + object.user_document_name,
}).fetch('GET', urlVal, {
//some headers ..
}).then((res) => {
// the path should be dirs.DocumentDir + 'path-to-file.anything'
let xVal = res.path();
console.log('The file saved to ', res.path());
})
})
}
I am facing one issue here
- (void) URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session is not getting called. Can anyone help with this?
In my project Appdelegate : AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,NSURLSessionDelegate>
@property (nonatomic, strong) UIWindow *window; @property (nonatomic, strong) void(^backgroundTransferCompletionHandler)(); @end
AppDelegate.m
//The backgroundTransferCompletionHandler is the one that is called when all downloads are complete
-
(void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {
self.backgroundTransferCompletionHandler = completionHandler;
}
There are a lot of reasons why URLSessionDidFinishEventsForBackgroundURLSession
might not get called. This looks like a reasonably thorough post on the topic. Hope that helps!
@danielsuo Thanks for the prompt reply. Any corrections to the changes I have made?. If possible, could you share your part of changes which you made to get it working?
I actually don't call URLSessionDidFinishEventsForBackgroundURLSession
and in my code I only changed one line (essentially turned the data task into an upload task). As I mentioned earlier in this thread, I didn't have time to wrap up nicely. Sorry this isn't more helpful!
@danielsuo Thanks for your inputs
Any updates?
The connection works fine in BG. However, URLSessionDidFinishEventsForBackgroundURLSession is still not called for me. You may refer to the updated code shared by me. I believe, https://stackoverflow.com/questions/32676352/urlsessiondidfinisheventsforbackgroundurlsession-not-calling-objective-c can possibly be looked at to fix the issue. For now, I have skipped that
I have the issue and I didn't find a solution yet. If you have any update, let me know! I've tried both configurations (background task and download task), with ios background fetch enabled but nothing solved.
I have a patched version based on the solution from @iosfitness , but the download progress was broken (referencing bytesWritten / totalBytesWritten rather than totalBytesWritten / totalBytesExpectedToSend)
https://github.com/tmaly1980/react-native-fetch-blob
I've created a pull request to have it merged into the new repository at: https://github.com/joltup/react-native-fetch-blob