Skip to content

Do custom ImageLoaders need to manage cache entries themselves? #2044

Answered by treitter
treitter asked this question in Q&A
Discussion options

You must be logged in to vote

I realized there's a test case which answers this question. Yes, there's built-in support for reading and writing from/to disk. My solution, including image compression on disk and converting all the way from/to a Drawable:

// NOTE: as of this writing, this is required:
// @OptIn(ExperimentalCoilApi::class)
inline fun <T : DiskCache.Editor?, R> T.use(block: (T) -> R): R {
    try {
        return block(this).also { this?.commit() }
    } catch (e: Exception) {
        this?.abort()
        throw e
    }
}
    
// NOTE: as of this writing, this is required:
// @OptIn(ExperimentalCoilApi::class)
private fun drawableFromDiskCache(diskCacheKey: String?): Drawable? {
    diskCacheKey ?: return…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by treitter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant