2017-04-26 5 views
1

로컬로 여러 MySqlite * .db 파일을 저장하는 응용 프로그램을 만들려고합니다. 앱을 데이터로 채우는 데 사용됩니다.React Native Background 다운로드

응용 프로그램은 이러한 파일 중 하나에 대해 최신 버전이 있는지 정기적으로 확인하고, 그렇다면 다운로드하여 파일의 이전 버전을 바꾸어야합니다. 이 업데이트 프로세스는 앱이 비활성 상태이거나 닫힌 상태에서 백그라운드에서 수행되어야합니다.

백그라운드 (예 : https://www.npmjs.com/package/react-native-background-task)에서 작업을 실행하는 데 사용할 수있는 몇 가지 플러그인을 보았습니다. 이것은 정기적으로 업데이트를 확인하는 데 사용할 수 있지만 iOS의 30 초 제한은 * .db 파일을 다운로드하기에 충분하지 않을 수 있습니다. 또한 플러그인은 최소 Android API 버전을 21로 설정합니다.

제 질문은 : 백그라운드에서 업데이트를 폴링하고 다운로드하여 이전 파일을 대체 할 수 있습니까?

답변

1

유용한 플러그인을 발견했습니다.

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

+1 RNFS 예. 내 경험에 비추어 볼 때 RNFetchBlob은 두 가지 이유로 불안정합니다. 1. 유지되지 않습니다. 2. 대용량 파일 다운로드 중에 JS 스레드를 차단하여 프로세스가 끝날 때까지 앱을 고정시킵니다. –