question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Removing requests from database when completed.

See original GitHub issue

Hi,

I have two questions.

  1. How to download a file twice with same url and file path? For example there is a song under url = “www.url.com/newSet.mp3” and I want to save it to “…/DOWNLOAD_PATH/newSet” path. User fetched the file for the first time…then deleted it…and then fetched again. Unfortunately it is impossible because I get error REQUEST_WITH_ID_ALREADY_EXIST.

The only solution that worked for me was calling fetch.delete(id); or fetch.remove(id); before each download. Do you need to store requests in database that succeed or failed? What about removing them just after download succeed or failed?

  1. How to download from two different urls to same directory? So same scenario like above but we have two different urls. Firstly we fetch from the first one…then delete the file…then download from second url. I get error like REQUEST_WITH_FILE_PATH_ALREADY_EXIST. Unfortunately I don’t have solution for this. The only thing that comes to my mind is to remove all Requests immediately when they completes download (success or error). But why it cannot be done by library?

Thanks in advance for your time 😃

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
tonyofranciscommented, Apr 29, 2018

@marcin-adamczewski

Question 1. Reason for Fetch’s behavior By default, Fetch stores all request in a database. Unless you provide your own ID for a request, Fetch uses a request’s url + file path to create a unique ID. If that unique ID is already found in the Fetch database, Fetch will not enqueue a new request with the same information. Also, Fetch will not enqueue any new request with a File Path that already exist in the database for another request. The reason for this is because we do not want two request that are downloading with the same file path try to save data in that one file. This would cause data corruption. In the case of a user deleting the file outside your app, Fetch will still maintain a record of the request for that downloaded file. You must remove the existing request first before enqueuing it again.

Solution 1: Remove exiting request and enqueue again

    @Override
    public void onCreate() {
        super.onCreate();
        final Request request = new Request("some url","some file path");
       //Check if requests exist first
        fetch.getDownload(request.getId(), new Func2<Download>() {
            @Override
            public void call(@Nullable Download download) {
                if (download == null) {
                    //Request does not exist enqueue new request
                    enqueueRequest(request);
                } else {
                    //Request does exist. Delete it and enqueue the new request
                    fetch.delete(request.getId());
                    enqueueRequest(request);
                }
            }
        });
    }
    
    private void enqueueRequest(Request request) {
        fetch.enqueue(request, new Func<Download>() {
            @Override
            public void call(@NotNull Download download) {
                //Request enqueued Successfully
            }
        }, new Func<Error>() {
            @Override
            public void call(@NotNull Error error) {
                //Error occurred enqueuing request.
            }
        });
    }

Solution 2: If you do not want Fetch to permanently store request data to a database on the local storage disk, you can create a temporary in memory database. Fetch will not persist any data from an in memory temporary database when the app is closed. Get a new Instance of Fetch like the following:

  final Fetch fetch = new Fetch.Builder(this, "namespace")
                .enabledInMemoryDatabase(true) 
                .build();

Solution 3: You can simply delete a request from Fetch when a download completes or fails.

Question 2 The information I provided in question 1 about Fetch’s behaviors answers this question in part. The reason why Fetch was designed with a database is to help resume incomplete downloads and to also provide you with request information when needed. You should delete request information from Fetch when no longer needed.

What I can do is to add a new option on the Fetch Builder that you can enable to auto delete records from the Fetch database when a download completes or fails after Fetch has reported this information to the Fetch Listeners.

Let me know if this helps or if you have more questions

2reactions
marcin-adamczewskicommented, Apr 29, 2018

@tonyofrancis Wow, you’re fast 😃 I like the customisations you introduced. I’ll test it tomorrow 😃 Nevertheless I think that if you want to not make options like AUTO_REMOVE_ON_COMPLETED and AUTO_REMOVE_ON_FAILED default, then such behaviour should be mentioned in Wiki so people are not confused and won’t get into trouble.

Thanks 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove completed move requests Exchange - ALI TAJRAN
Remove completed move requests with a single command through Exchange Management Shell. How to do it? Learn more in this article.
Read more >
Removing a database with old export requests
I'm getting an error saying that I can't remove the database since "This mailbox database is associated with one or more active MailboxExport ......
Read more >
Using a delete request to deleting archived data - IBM
You can remove data from the database by a creating and running a separate delete request, or data can be deleted during the...
Read more >
How to Remove Yourself from National Database - DeleteMe
DeleteMe's National Database Review​​ Removing yourself from InfoTracer requires you to perform their online opt-out process, and then verify ...
Read more >
Part 23 Why deleting database records using get request is bad
In this video we will discuss, why deleting database records using GET request is bad. Please watch Part 22, before proceeding.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found