FileDownloader icon indicating copy to clipboard operation
FileDownloader copied to clipboard

一些优化想法

Open xns1001 opened this issue 10 years ago • 9 comments

1:下载线程池的最大并行数写死为3感觉不够灵活,我觉得你可以把这个设置开放出来方便用户调整 2:更新进度时每次都要update数据库,会比较耗时吧。我觉得可以只在pause和出现异常等时刻再写入数据库可以更好一些

个人想法,还望指教!

xns1001 avatar Mar 14 '16 10:03 xns1001

恩。谢谢你的建议。:)

你所提到这些点,我在早期的时候有思考过:

第一点:

资源是有限的,如何最大限度的将资源调度起来,这边参考的是 Fresco的下载线程数( HttpUrlConnectionNetworkFetcher (Fresco也没有对外开放),Fresco在Pipeline以及资源合理利用研究方面行业有名的,你懂的。

第二点:

这边主要是当心下载文件被外界修改,或者下载进程crash了等,为了鲁棒性权衡的结果,当然,如果你有研究过 android.app.DownloadManager 它也是这么做的,性能方面其实都是权衡的结果,但是确实有优化的空间,只不过可能复杂度会非常高。

Jacksgong avatar Mar 14 '16 10:03 Jacksgong

是否可以根据手机设备当前的网络来动态调整线程池的大小呢?picasso的实现方案

TUBB avatar Mar 15 '16 08:03 TUBB

package com.liulishuo.filedownloader.services;
class FileDownloadRunnable

private void onProcess(final long soFar, final long total) {
        if (soFar != total) {
            downloadTransfer.setSoFarBytes(soFar);
            downloadTransfer.setTotalBytes(total);
            downloadTransfer.setStatus(FileDownloadStatus.progress);

            helper.update(downloadTransfer.getDownloadId(), FileDownloadStatus.progress, soFar, total);
        }

        if (maxNotifyBytes < 0 || soFar - lastNotifiedSoFar < maxNotifyBytes) {
            return;
        }

        lastNotifiedSoFar = soFar;
        if (FileDownloadLog.NEED_LOG) {
            FileDownloadLog.d(this, "On progress %d %d %d", downloadTransfer.getDownloadId(), soFar, total);
        }


        FileDownloadProcessEventPool.getImpl().asyncPublishInNewThread(event.setTransfer(downloadTransfer));

    }

以上是每一次更新进度都会update数据库的代码,

helper.update(downloadTransfer.getDownloadId(), FileDownloadStatus.progress, soFar, total);

这个调用,不知是否可以移到

lastNotifiedSoFar = soFar;

这一句之前? 这样做会对其他功能造成影响吗?是否会影响断点续传? 盼望回复,谢谢。

luojin avatar Mar 20 '16 05:03 luojin

@luojin 有问题回复到这里。

Jacksgong avatar Mar 21 '16 03:03 Jacksgong

@luojin 数据库的问题,我已经说过了。你看我上面的回复。

Jacksgong avatar Mar 21 '16 03:03 Jacksgong

@luojin

这边主要是当心下载文件被外界修改,或者下载进程crash了等,为了鲁棒性权衡的结果,当然,如果你有研究过 android.app.DownloadManager 它也是这么做的,性能方面其实都是权衡的结果,但是确实有优化的空间,只不过可能复杂度会非常高。

Jacksgong avatar Mar 21 '16 03:03 Jacksgong

helper.update(downloadTransfer.getDownloadId(), FileDownloadStatus.progress, soFar, total);

这个调用,不知是否可以移到

lastNotifiedSoFar = soFar;

这一句之前? 这样做会对其他功能造成影响吗?是否会影响断点续传? @Jacksgong

luojin avatar Mar 21 '16 07:03 luojin

@luojin 可以的,但是这样,数据库 progress 更新的频次就会受到了 业务层的影响( BaseDownloadTask#setCallbackProgressTimes)

Jacksgong avatar Apr 04 '16 15:04 Jacksgong

@DubheX #112

Jacksgong avatar Apr 16 '16 10:04 Jacksgong