fluttercommunity / fluttercommunity/flutter_downloader

Persist a field to map back from a DownloadTask to the app own's entity

Open
#402 2 comments 4 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
940
Forks
561
Avg merge
1h 18m
Merged PRs (30d)
1

Description

Hi,

Here is a scenario for which I don't see a simple solution:

1. The user enqueues a list of tasks, from a list of `Books`. A map `taskId` → `Book.id` is kept somewhere in the app, but not persisted on the device.
2. The progress of downloads is monitored and the books are updated using the map.
3. The user leaves the app so the app goes to background and is later killed by the os. Meanwhile the downloads should continue (right?).
4. The user comes back to the app. The download state (the `taskId` → `Book.id` map) is lost, so the app get all the tasks using `loadTasks()` to correctly display the download states.
5. But, a `DownloadTask` doesn't have a field that can help in mapping back this task to a `Book`. Relying on fields such as `filename` or `url` is not robust as these may change (for example if the url contains changing GET parameters, of if the filename changes).

If in 1. we had persisted the map on the device, then this wouldn't be a problem, we would load it in 5. and would be able to map task states to Books.

But, it's somewhat awkward to have to do this since the plugin maintains a database entirely dedicated to storing the download queue state.

The above problem could be solved by adding a `DownloadTask.extra` field (`dynamic` or `String`) and save it to the database. Then, the `Book.id` would be stored into it (set in the `enqueue` method) and retrieved with `loadTasks()` or `loadTasksWithRawQuery()`. Ideally the callback would also reveive it as a fourth parameter upon status updates.

_Note: I was initially considering using the `headers` field to do so (by adding an `X-Book-Id` header for example), but this field is only settable from the `enqueue` method and not present in the `DownloadTask` class, although it's part of the database record. Also adding it in `DownloadTask` could be usefull._

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.