便利なプラグインがいくつか見つかりました。
1.反応し、ネイティブ・フェッチ・ブロブ
https://github.com/wkh237/react-native-fetch-blob/wiki/Classes#rnfetchblobconfig
それはIOSBackgroundTask
オプションがあります。
RNFetchBlob
.config({
path : dest_file_path,
IOSBackgroundTask: true,
overwrite: true,
indicator: true,
})
.fetch('GET', download_url, {
//some headers ..
})
.progress((received, total) => {
console.log('progress : '+ received + '/' + total);
})
.then((res) => {
console.log('# The file saved to :', file_path);
})
ところで、外見は正しく動作しません。 私は何か...
2.反応ネイティブ-FSを
https://github.com/itinance/react-native-fs#downloadfileoptions-downloadfileoptions--jobid-number-promise-promisedownloadresult-
const ret = RNFS.downloadFile({
fromUrl: download_url,
toFile: dest_file_path,
connectionTimeout: 1000 * 10,
background: true,
discretionary: true,
progressDivider: 1,
resumable: (res) => {
console.log("# resumable :", res);
},
begin: (res) => {
// start event
},
progress: (data) => {
const percentage = ((100 * data.bytesWritten)/data.contentLength) | 0;
console.log("# percentage :", percentage);
},
});
jobId = ret.jobId;
ret.promise.then((res) => {
console.log('Download finished.');
RNFS.completeHandlerIOS(jobId);
jobId = -1;
}).catch(err => {
console.log('error');
jobId = -1;
});
を逃した場合にそれがうまく働いて見えわかりません。
ところで、プッシュ通知でバックグラウンドでダウンロードしようとすると、アプリを開かない限りダウンロードが開始されません。
誰でもこの問題を解決できますか?
+1とRNFSの例。私の経験から、RNFetchBlobは2つの理由から不安定な選択です。 1.維持されていない。 2.大量のファイルをダウンロードする際にJSスレッドをブロックし、プロセスが終了するまでアプリをフリーズします。 –