React-native-fs: downloadFile() when app goes into the background

Created on 22 Apr 2016  路  8Comments  路  Source: itinance/react-native-fs

I'm finding that for some large file downloads, the app going into the background (or the phone locking due to inactivity) is causing downloads via downloadFile() to fail with an error. Is there any workaround to support background downloads?

question

Most helpful comment

Closing as this is possible as of version 1.5.0

All 8 comments

@af this is a limitation of iOS - when your app goes into background any activity is stopped. What is the exact error you are receiving?

I don't have the error handy unfortunately (I'm not at that computer at the moment). However, it looks like the ability to download in the background is now available with a new(ish) iOS API:

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
https://realm.io/news/gwendolyn-weston-ios-background-networking/

Not sure how much it would take to use that NSURLSession API for downloads instead of the existing NSURLConnection one (I'm an iOS noob). Does it look feasible with the existing RNFS design?

@af It will require some code changes but without looking further into it I'd assume it is definitely possible. A downside I see is that it looks like iOS then will re-launch your app when the download completes which might not be a behavior people expect by default. (I might be reading that one wrong though).

Cool! Glad to know it looks like it's possible. On the topic of re-launching, my reading of the docs suggests that's only the case when the sessionSendsLaunchEvents property is set to YES. That's just my interpretation though, could be incorrect

@johanneslumpe I might be able to put together a PR for this later this week. Is it a reasonable approach to move all downloads to NSURLSession, or should there be an opt-in to this behaviour?

@af I think this should be opt-in behaviour. Android has a background download service of sorts so it might be possible to have an elegant cross platform abstraction. Others have suggested making downloadFile more capable so I'm debating having arguments passed as an options object so we can have optional things like headers as well. That would work well for this feature because we could have { background: true } as an option.

@cjdell Sorry, it took me longer than expected to be able to put together a PR for this! #100 contains an implementation of this feature, triggered by passing { background: true } as an extra param to downloadFile() as you suggested.

Closing as this is possible as of version 1.5.0

Was this page helpful?
0 / 5 - 0 ratings