author:wangyimiao

desc:清理缓存协程修复
master
yimiao 3 years ago
parent b74b0cd103
commit b6738d6357

@ -69,6 +69,8 @@ public class MainActivity extends AppCompatActivity {
}); });
}); });
binding.clearBtn.setOnClickListener(l -> ImageLoader.INSTANCE.clearAllCache(MainActivity.this));
binding.btn.setOnClickListener(l -> { binding.btn.setOnClickListener(l -> {
List<RequestHeadInterceptor> interceptors = new ArrayList<>(); List<RequestHeadInterceptor> interceptors = new ArrayList<>();
RequestHeadInterceptor.Builder builder = new RequestHeadInterceptor.Builder(); RequestHeadInterceptor.Builder builder = new RequestHeadInterceptor.Builder();

@ -15,7 +15,7 @@
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.2" /> app:layout_constraintVertical_bias="0.2" />
<TextView <Button
android:id="@+id/tv" android:id="@+id/tv"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -23,7 +23,8 @@
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.53" />
<Button <Button
android:id="@+id/btn" android:id="@+id/btn"
@ -31,10 +32,11 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="get collections" android:text="get collections"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.599" /> app:layout_constraintVertical_bias="0.6" />
<Button <Button
android:id="@+id/login_btn" android:id="@+id/login_btn"
@ -47,4 +49,15 @@
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.690" /> app:layout_constraintVertical_bias="0.690" />
<Button
android:id="@+id/clear_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clear"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.770" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

@ -2,6 +2,7 @@ package com.common.commonlib.image.loader
import android.content.Context import android.content.Context
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.util.Log
import android.widget.ImageView import android.widget.ImageView
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder import com.bumptech.glide.RequestBuilder
@ -11,6 +12,7 @@ import com.common.commonlib.image.config.GlideBaseConfig
import com.common.commonlib.image.config.GlideHolderConfig import com.common.commonlib.image.config.GlideHolderConfig
import com.common.commonlib.image.config.GlideTransitionConfig import com.common.commonlib.image.config.GlideTransitionConfig
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@ -77,22 +79,22 @@ object ImageLoader : ILoader {
} }
override fun clearMemoryCache(context: Context) = runBlocking<Unit> { override fun clearMemoryCache(context: Context) = runBlocking<Unit> {
launch(Dispatchers.Main) { GlobalScope.launch(Dispatchers.Main) {
Glide.get(context).clearMemory() Glide.get(context).clearMemory()
} }
} }
override fun clearDiskCache(context: Context) = runBlocking<Unit> { override fun clearDiskCache(context: Context) = runBlocking<Unit> {
launch(Dispatchers.IO) { launch(Dispatchers.Default) {
Glide.get(context).clearDiskCache() Glide.get(context).clearDiskCache()
} }
} }
override fun clearAllCache(context: Context) = runBlocking<Unit> { override fun clearAllCache(context: Context) = runBlocking<Unit> {
launch(Dispatchers.Main) { GlobalScope.launch(Dispatchers.Main) {
Glide.get(context).clearMemory() Glide.get(context).clearMemory()
} }
launch(Dispatchers.IO) { launch(Dispatchers.Default) {
Glide.get(context).clearDiskCache() Glide.get(context).clearDiskCache()
} }
} }

Loading…
Cancel
Save