react-native-fetch-blob icon indicating copy to clipboard operation
react-native-fetch-blob copied to clipboard

iOS: Lost connection to background transfer service

Open ptelad opened this issue 7 years ago • 13 comments

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.

ptelad avatar Jul 30 '17 16:07 ptelad

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.

jmparsons avatar Aug 23 '17 18:08 jmparsons

Same for me!

mcatmos avatar Jan 09 '18 14:01 mcatmos

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

ZionChang avatar Jan 11 '18 06:01 ZionChang

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 avatar Feb 13 '18 18:02 danielsuo

@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

iosfitness avatar Mar 18 '18 10:03 iosfitness

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 avatar Mar 18 '18 10:03 danielsuo

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

iosfitness avatar Mar 18 '18 10:03 iosfitness

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 avatar Mar 18 '18 10:03 danielsuo

@danielsuo Thanks for your inputs

iosfitness avatar Mar 18 '18 10:03 iosfitness

Any updates?

speedandfunction-anri avatar Apr 18 '18 16:04 speedandfunction-anri

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

iosfitness avatar Apr 18 '18 16:04 iosfitness

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.

matteodanelli avatar Apr 24 '18 10:04 matteodanelli

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

tmaly1980 avatar May 21 '18 16:05 tmaly1980