Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure RealImageLoader is fully initialized before registering system callbacks. #1465

Merged
merged 2 commits into from Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -13,6 +13,7 @@ import coil.memory.MemoryCache.Key
import coil.memory.MemoryCache.Value
import org.junit.Before
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class SystemCallbacksTest {
Expand Down Expand Up @@ -56,10 +57,10 @@ class SystemCallbacksTest {
memoryCache[Key("1")] = Value(createBitmap(1000, 1000, Bitmap.Config.ARGB_8888))
memoryCache[Key("2")] = Value(createBitmap(1000, 1000, Bitmap.Config.ARGB_8888))

assertTrue(memoryCache.size == 8000000)
assertEquals(8_000_000, memoryCache.size)

systemCallbacks.onTrimMemory(TRIM_MEMORY_COMPLETE)

assertTrue(memoryCache.size == 0)
assertEquals(0, memoryCache.size)
}
}
9 changes: 6 additions & 3 deletions coil-base/src/main/java/coil/RealImageLoader.kt
Expand Up @@ -112,6 +112,11 @@ internal class RealImageLoader(
EngineInterceptor(this, requestService, logger)
private val isShutdown = AtomicBoolean(false)

init {
// Must be called only after the image loader is fully initialized.
systemCallbacks.register()
}

override fun enqueue(request: ImageRequest): Disposable {
// Start executing the request on the main thread.
val job = scope.async {
Expand Down Expand Up @@ -213,9 +218,7 @@ internal class RealImageLoader(
@Suppress("SAFE_CALL_WILL_CHANGE_NULLABILITY", "UNNECESSARY_SAFE_CALL")
internal fun onTrimMemory(level: Int) {
// https://github.com/coil-kt/coil/issues/1443
try {
memoryCacheLazy?.value?.trimMemory(level)
} catch (_: NullPointerException) {}
memoryCacheLazy?.value?.trimMemory(level)
}

override fun shutdown() {
Expand Down
2 changes: 1 addition & 1 deletion coil-base/src/main/java/coil/util/SystemCallbacks.kt
Expand Up @@ -39,7 +39,7 @@ internal class SystemCallbacks(
val isOnline get() = _isOnline
val isShutdown get() = _isShutdown.get()

init {
fun register() {
context.registerComponentCallbacks(this)
}

Expand Down