Fetch: Delete Function not working

Created on 20 May 2018  路  4Comments  路  Source: tonyofrancis/Fetch

Hey,i was using deleteAll() function for deleting all my downloaded files but it seams it doesn't delete them from storage it says that Cleared Database Main.db but when i check the storage files are still there can you check this to see whats the problem?

help wanted

All 4 comments

@Moeinh77 Just tested the deleteAll() method and all the requests and downloaded files were deleted off the devices. Make sure that M+ devices have accepted the read/write permission before deleting. Fetch is not able to delete files if the permission is not granted. If that's not the case. Where in your code are you calling the delete method?.

@tonyofrancis
This is my code
i make an object from this class and i do my things with it but when i call deleteAllFiles the deleteAll function doesn't do the job i wonder why

class download_controller(val activity: Activity) {

var UriList=ArrayList<Uri>()

private var RequestList=ArrayList<Request>()

private val okHttpClient = OkHttpClient.Builder().build()

private var GroupmainFetch:Fetch

private val fetchListener = object : FetchListener {

    override fun onQueued(download: Download) {
        Log.v("Queued",download.tag.toString()+"queued")
    }

    override fun onCompleted(download: Download) {

        Log.v("Completed",download.tag.toString()+"completed")

        UriList.add(Uri.parse(download.file))

        Log.v("Added to List",download.tag.toString()+"added to uri list")

    }

    override fun onError(download: Download) {



        Toast.makeText(activity,download.file+
                "  Error downloading file: "+download.file
                , Toast.LENGTH_SHORT).show()
    }

    override fun onProgress(download: Download, etaInMilliSeconds: Long, downloadedBytesPerSecond: Long) {

        val progress = download.progress
        Log.d("Fetch", download.tag.toString() + " Progress Completed :" + progress)
    }


    override fun onDeleted(download: Download) {

        Log.v("Deleted!!!",download.file)

    }
}
init {
    GroupmainFetch= Fetch.Builder(activity, "Main")
            .setDownloader(OkHttpDownloader(okHttpClient))
            .setDownloadConcurrentLimit(1) 
            .enableLogging(true)
            .addRequestOptions(RequestOptions.AUTO_REMOVE_ON_COMPLETED)
            .build()
    GroupmainFetch.addListener(fetchListener)

}

fun addRequest(url:String,tag:String){

    val download_address=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()

    val request= Request(url,download_address +"/"+tag)
    request.tag=tag

    RequestList.add(request)

    Log.v("Request",tag+" added to requests!!!")
}

fun startDownload(){

    GroupmainFetch.enqueue(RequestList
            , object : Func<List<Download>> {
        override fun call(t: List<Download>) {

            UriList.add(Uri.parse(t.get(0).file))
            UriList.add(Uri.parse(t.get(1).file))

            Toast.makeText(activity,
                    "Started the list download",
                    Toast.LENGTH_SHORT).show()

        }
    }, object : Func<Error> {
        override fun call(t: Error) {
            Toast.makeText(activity,
                    "Error in list download"+t.toString(),
                    Toast.LENGTH_SHORT).show()            }
    })


}

fun getDownlodedList():ArrayList<Uri>{

    return UriList

}

fun deleteAllFiles(){



    GroupmainFetch.deleteAll()
    GroupmainFetch.close()

    UriList.clear()

}

fun closeFetch(){

    GroupmainFetch.close()

}
}

@Moeinh77 the reason why the delete all method is not working is because of the RequestOptions.AUTO_REMOVE_ON_COMPLETED that is set. This option removes the request inside Fetch but does not delete the file. See the java docs. When deleteAll() method is called, the request is not found so the download cannot be deleted. You may want to try one of the other Enqueue RequestOptions.

@tonyofrancis
ah,understood
i guess REPLACE_ALL_ON_ENQUEUE_WHERE_UNIQUE will do the job

Was this page helpful?
0 / 5 - 0 ratings