在 React Native 应用程序中重复调用回调函数时非常慢

It is so slow when a callback function be called repeatedly in react native app

当我开始下载任务时,它会重复调用一个回调函数。但是它会很慢,以至于触摸按钮时无法提供反馈。

            Fileio.downloadFile(downloadData[downloadFileIndex].uri, '1.jpg',this.progressFunc.bind(this)).then((DownloadResult)=> {
                if (DownloadResult.statusCode == 200) {
                    let nextDownloadFileIndex = this.props.downloadFileIndex + 1;
                    this.props.dispatch(DOWNLOADED_A_FILE({
                        downloadFileIndex:nextDownloadFileIndex,
                        progressNum:0
                }))
                }
            }).catch((error)=> {
                console.log(error)
            })

这是我的代码,回调函数如下

    progressFunc(DownloadBeginCallbackResult) {
    let progressNum = DownloadBeginCallbackResult.bytesWritten / DownloadBeginCallbackResult.contentLength;
    if(progressNum<=0.99){
        this.props.dispatch(DOWNLOADING_A_FILE({
            progressNum:progressNum,
            jobId:this.props.jobId
        }));
    }else{
        this.props.dispatch(DOWNLOADING_A_FILE({
            progressNum:0,
            jobId:this.props.jobId
        }));
    }
}

我的意思是当我触摸按钮时我不能立即得到反馈。我认为这是因为我有一个重复调用的回调函数。所以js处理不了那么多任务;

听起来 JS 线程正忙于执行请求,无法与 UI 线程通信。您可以尝试的一件事是将您包裹在 InteractionManager.runAfterInteractions(() => ...) 中的印刷机处理程序中 参见 https://facebook.github.io/react-native/docs/interactionmanager.html