Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

iOS: Lost connection to background transfer service #448

Open
ptelad opened this issue Jul 30, 2017 · 13 comments
Open

iOS: Lost connection to background transfer service #448

ptelad opened this issue Jul 30, 2017 · 13 comments
Labels

Comments

@ptelad
Copy link

ptelad commented Jul 30, 2017

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.

@lll000111 lll000111 added the ios label Aug 21, 2017
@jmparsons
Copy link

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.

@mcatmos
Copy link

mcatmos commented Jan 9, 2018

Same for me!

@ZionChang
Copy link

+1
when the system kill the app, I think it would better to allow app keep downloading

@danielsuo
Copy link

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.

@iosfitness
Copy link

iosfitness commented Mar 18, 2018

@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;

}

RNFetchBlobNetwork updated files.zip

@danielsuo
Copy link

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!

@iosfitness
Copy link

@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?

@danielsuo
Copy link

danielsuo commented Mar 18, 2018

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!

@iosfitness
Copy link

@danielsuo Thanks for your inputs

@speedandfunction-anri
Copy link

Any updates?

@iosfitness
Copy link

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

@matteodanelli
Copy link

matteodanelli commented Apr 24, 2018

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.

@tmaly1980
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

10 participants