ssseasonnn / RxDownload

A multi-threaded download tool written with RxJava and Kotlin
Apache License 2.0
4.14k stars 612 forks source link

最大下载数限制设置未生效? #359

Closed houkhan closed 1 year ago

houkhan commented 1 year ago

需求:设置 maxConCurrency = 1 失效了 我设置 maxConCurrency=1 当我多次调用 GameDownloadManager.start 开始多个任务仍会多个同时下载 我应该如何修改呢

我的代码如下

object GameDownloadManager {
    private const val TAG = "GameDownloadManager"
    private val downloadMap = mutableMapOf<String, TaskManager>()
    fun start(context: Context, downloadUrl: String, gameName: String, gameID: String, isJustDownload: Boolean = false) {
        LogUtils.dTag(TAG, "start download downloadUrl==$downloadUrl")
        // 校验字段是否合法
        if (downloadUrl.isEmpty()) {
            AppToast.showShortSafe(R.string.game_download_url_is_empty)
            return
        }
        if (gameName.isEmpty()) {
            AppToast.showShortSafe(R.string.game_download_game_name_is_empty)
            return
        }
        if (gameID.isEmpty()) {
            AppToast.showShortSafe(R.string.game_download_game_id_is_empty)
            return
        }
        // 下载逻辑
        var taskManager = downloadMap[downloadUrl]
        if (taskManager == null) {
            taskManager = Task(url = downloadUrl)
                .manager(maxConCurrency = 1, notificationCreator = GameDownloadNotificationCreator(context, gameID, gameName), recorder = RoomRecorder())
            downloadMap[downloadUrl] = taskManager
        }
        when (taskManager.currentStatus()) {

            is Normal -> taskManager.start()

            is Pending -> if (!isJustDownload) taskManager.delete()

            is Started -> if (!isJustDownload) taskManager.stop()

            is Downloading -> if (!isJustDownload) taskManager.stop()

            is Failed -> taskManager.start()

            is Paused -> taskManager.start()

            is Completed -> AppUtils.installApp(taskManager.file())

            is Deleted -> taskManager.start()
        }
        taskManager.subscribe { status ->
            // 此处监听下载完成自动触发安装
            when (status) {

                is Normal -> LogUtils.dTag(TAG, "Normal")

                is Pending -> LogUtils.dTag(TAG, "Pending")

                is Started -> LogUtils.dTag(TAG, "Started")

                is Downloading -> LogUtils.dTag(TAG, "Downloading")

                is Failed -> LogUtils.dTag(TAG, "Failed==" + status.throwable)

                is Paused -> LogUtils.dTag(TAG, "Paused")

                is Completed -> {
                    LogUtils.dTag(TAG, "Completed")
                    AppUtils.installApp(taskManager.file())
                }

                is Deleted -> LogUtils.dTag(TAG, "Deleted")
            }
        }
    }

    fun getDownloadTaskManager(downloadUrl: String): TaskManager? {
        return downloadMap[downloadUrl]
    }
}
Teemo100 commented 1 year ago

邮件已收到,三日内拜读

houkhan commented 1 year ago

我翻看历史issues 尝试这么修改 还是不行的

object GameDownloadManager {
    private const val TAG = "GameDownloadManager"
    private val downloadMap = mutableMapOf<String, TaskManager>()
    fun start(context: Context, downloadUrl: String, gameName: String, gameID: String, isJustDownload: Boolean = false) {
        LogUtils.dTag(TAG, "start download downloadUrl==$downloadUrl")
        // 校验字段是否合法
        if (downloadUrl.isEmpty()) {
            AppToast.showShortSafe(R.string.game_download_url_is_empty)
            return
        }
        if (gameName.isEmpty()) {
            AppToast.showShortSafe(R.string.game_download_game_name_is_empty)
            return
        }
        if (gameID.isEmpty()) {
            AppToast.showShortSafe(R.string.game_download_game_id_is_empty)
            return
        }
        // 下载逻辑
        var taskManager = downloadMap[downloadUrl]
        if (taskManager == null) {
            val taskLimitation = BasicTaskLimitation(1)
            taskManager = Task(url = downloadUrl)
                .manager(
                    taskLimitation = taskLimitation,
                    notificationCreator = GameDownloadNotificationCreator(context, gameID, gameName),
                    recorder = RoomRecorder()
                )
            downloadMap[downloadUrl] = taskManager
        }
        when (taskManager.currentStatus()) {

            is Normal -> taskManager.start()

            is Pending -> if (!isJustDownload) taskManager.delete()

            is Started -> if (!isJustDownload) taskManager.stop()

            is Downloading -> if (!isJustDownload) taskManager.stop()

            is Failed -> taskManager.start()

            is Paused -> taskManager.start()

            is Completed -> AppUtils.installApp(taskManager.file())

            is Deleted -> taskManager.start()
        }
        taskManager.subscribe { status ->
            // 此处监听下载完成自动触发安装
            when (status) {
                is Completed -> {
                    AppUtils.installApp(taskManager.file())
                }
            }
        }
    }

    fun getDownloadTaskManager(downloadUrl: String): TaskManager? {
        return downloadMap[downloadUrl]
    }
}

由于我的 通知栏 不能 默认创建 我需要传入不同的参数 我是这么写的 是这个原因创建了多个TaskManager 导致的吗

houkhan commented 1 year ago
val taskLimitation = BasicTaskLimitation(1)

这种方式我测试在demo中是正常的 我的是哪里有问题吗

ssseasonnn commented 1 year ago
        var taskManager = downloadMap[downloadUrl]
        if (taskManager == null) {
           // **BasicTaskLimitation改成单例,不要每次都创建新的对象**
            val taskLimitation = BasicTaskLimitation(1)
            taskManager = Task(url = downloadUrl)
                .manager(
                    taskLimitation = taskLimitation,
                    notificationCreator = GameDownloadNotificationCreator(context, gameID, gameName),
                    recorder = RoomRecorder()
                )
            downloadMap[downloadUrl] = taskManager
        }

BasicTaskLimitation改成单例,不要每次都创建新的对象

houkhan commented 1 year ago

请问下 这个字段是什么作用的 maxConCurrency 这个不是最大并发数吗

ssseasonnn commented 1 year ago

maxConCurrency 是控制每个Task内的下载线程数量 taskLimitation 是控制同时下载的Task数量

houkhan commented 1 year ago

好的 谢谢解惑 已解决 非常感谢