Fetch: Is there a way to remove request from Fetch's database

Created on 16 May 2017  路  8Comments  路  Source: tonyofrancis/Fetch

Assume that user downloaded a file and move that file to other folder or delete it, they cannot download that file again because it save into Fetch's database. Is there a way to let user re-download?

bug

Most helpful comment

@RealAction - Not sure if I am understanding correctly, but if you want to re-download the same file at the same Location, just do a fetch.retry(old_download_id). I am doing the same and it seems to work. Although having the file removed from Fetch DB easily is desirable, this workaround seems to work for me in both the cases.

All 8 comments

@RealAction The FetchService automatically removes the entry for a request if the file is moved or deleted. Can you provide me a sample app or log?

@RealAction disregard my previous answer. The following is how Fetch handles downloaded files that have been removed.

Every time the FetchService starts, its verifies the database by checking that the downloaded files still exist. If a file was deleted or cannot be accessed, the service sets the request status to Fetch.STATUS_ERROR with an error value of Fetch.ERROR_FILE_NOT_FOUND.

What you can do is query fetch for the request and check its status. If the status is invalid, remove the request and enqueue again. Example:

        final Request request = new Request("url","dirPath","fileName");
        final RequestInfo requestInfo = fetch.get(request);

        if(requestInfo != null && requestInfo.getStatus() == Fetch.STATUS_ERROR
                && requestInfo.getError() == Fetch.ERROR_FILE_NOT_FOUND) {

            //Listen for when the request has be removed
            fetch.addFetchListener(new FetchListener() {
                @Override
                public void onUpdate(long id, int status, int progress, long downloadedBytes, long fileSize, int error) {

                    if(id == requestInfo.getId() && status == Fetch.STATUS_REMOVED) {
                        //Enqueue request
                        long downloadId = fetch.enqueue(request);

                        //Save downloadId....

                        fetch.removeFetchListener(this);
                    }
                }
            });

            fetch.remove(requestInfo.getId());
        }else {
           long downloadId =  fetch.enqueue(request); //Save downloadId....
        }

Let me know if this works how you except.

@tonyofrancis
i made simple example so you can see issue: https://github.com/DevOfLife/TestDownload5
step:

  • click into folder icon to download the sample image (located on Downloads folder of sd card)
  • close the app
  • Delete the downloaded image
  • then re-open the app
  • click into folder icon again to download

result log:
05-19 22:28:34.359 8084-8084/com.devoflife.app.testdownload E/aaaa: aaaa:904 //status
05-19 22:28:34.359 8084-8084/com.devoflife.app.testdownload E/aaaa: aaaa:-111 //error
05-19 22:28:34.363 8084-8084/com.devoflife.app.testdownload W/System.err: com.tonyodev.fetch.exception.EnqueueException: DatabaseHelper already containsFilePath a request with the filePath:/storage/emulated/0/Download/pic_mountain.jpg
05-19 22:28:34.363 8084-8084/com.devoflife.app.testdownload W/System.err: at com.tonyodev.fetch.DatabaseHelper.getInsertStatement(DatabaseHelper.java:147)
05-19 22:28:34.363 8084-8084/com.devoflife.app.testdownload W/System.err: at com.tonyodev.fetch.DatabaseHelper.insert(DatabaseHelper.java:166)
05-19 22:28:34.363 8084-8084/com.devoflife.app.testdownload W/System.err: at com.tonyodev.fetch.Fetch.enqueue(Fetch.java:292)
05-19 22:28:34.363 8084-8084/com.devoflife.app.testdownload W/System.err: at com.devoflife.app.testdownload.PresentationAdapter.ListDownloadAdapter$3.onClick(ListDownloadAdapter.java:106)
05-19 22:28:34.363 8084-8084/com.devoflife.app.testdownload W/System.err: at android.view.View.performClick(View.java:5610)
05-19 22:28:34.363 8084-8084/com.devoflife.app.testdownload W/System.err: at android.view.View$PerformClick.run(View.java:22265)
05-19 22:28:34.363 8084-8084/com.devoflife.app.testdownload W/System.err: at android.os.Handler.handleCallback(Handler.java:751)
05-19 22:28:34.363 8084-8084/com.devoflife.app.testdownload W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
05-19 22:28:34.363 8084-8084/com.devoflife.app.testdownload W/System.err: at android.os.Looper.loop(Looper.java:154)
05-19 22:28:34.363 8084-8084/com.devoflife.app.testdownload W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6077)
05-19 22:28:34.363 8084-8084/com.devoflife.app.testdownload W/System.err: at java.lang.reflect.Method.invoke(Native Method)
05-19 22:28:34.363 8084-8084/com.devoflife.app.testdownload W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
05-19 22:28:34.363 8084-8084/com.devoflife.app.testdownload W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Seems like there is a bug. I am looking into it

@RealAction Can you try this work around and let me know if it works for you? I am still looking into this issue.

Work Around:

          DownloadInfo toDownload= mListDownloadInfo.get(holder.getAdapterPosition());
                final Request request = new Request(toDownload.getUrl()
                        , Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()
                        ,toDownload.getFileName());

                final RequestInfo requestInfo = mFetchDownloader.get(request);

                if(requestInfo != null && requestInfo.getError() == Fetch.ERROR_FILE_NOT_FOUND) {

                    Log.e("aaaa", "aaaa:"+requestInfo.getStatus());
                    Log.e("aaaa", "aaaa:"+requestInfo.getError());

                    mFetchDownloader.addFetchListener(new FetchListener() {
                        @Override
                        public void onUpdate(long id, int status, int progress, long downloadedBytes, long fileSize, int error) {

                            if(id == requestInfo.getId() && status == Fetch.STATUS_REMOVED) {

                                mFetchDownloader.enqueue(request);
                                mFetchDownloader.removeFetchListener(this);
                            }
                        }
                    });

                    mFetchDownloader.remove(requestInfo.getId());
                }else {
                    toDownload.setDownloadId(mFetchDownloader.enqueue(request));
                }

the code you post above fix half of issues, that make me confuse. Let me explain in 2 script:
NOTICE:

  • in both 2 script Fetch will release then re-instance before user download
  • i put 2 line "aaaa" log outsize of if block

Script 1 (success):

  • Download the file
  • User manually kill app completely (by click phone's menu button then slide to kill app)
  • Delete the downloaded file
  • User open app again then download
  • Result: work perfect (download again success)

Script 2 (not success):

  • Download the file
  • User kill app (by click back button until app close)
  • Delete the downloaded file
  • User open app again then download
  • Result:
05-23 21:52:44.679 14855-14855/com.devoflife.app.testdownload E/aaaa: aaaa:903 //status
05-23 21:52:44.679 14855-14855/com.devoflife.app.testdownload E/aaaa: aaaa:-1 //error
05-23 21:52:44.680 14855-14855/com.devoflife.app.testdownload W/System.err: com.tonyodev.fetch.exception.EnqueueException: DatabaseHelper already containsFilePath a request with the filePath:/storage/emulated/0/Download/pic_mountain.jpg

HI
if I want to update an downloaded file, Fetch don't allow me! and return -1 as download id.
I think that u must add a method to remove download data from fetch database without deleting the file.
if i use fetch.RemoveAll() it will work good but this method will delete my old file. so if my updat crash, then i have no file!

@RealAction - Not sure if I am understanding correctly, but if you want to re-download the same file at the same Location, just do a fetch.retry(old_download_id). I am doing the same and it seems to work. Although having the file removed from Fetch DB easily is desirable, this workaround seems to work for me in both the cases.

Was this page helpful?
0 / 5 - 0 ratings