@InstallIn(SingletonComponent::class)
object NetworkModule {
@Provides
@Singleton
@BaseClientWithOnlyCacheInterceptor
fun provideOkHttpClientWithCacheInterceptor(
@BaseClientWithNoInterceptor okHttpClient: OkHttpClient,
cacheHeaderInterceptor: CacheHeaderInterceptor,
cacheConfigProvider: AppCacheConfigProvider
): OkHttpClient {
val builder = okHttpClient.newBuilder()
.cacheConfig(cacheConfigProvider.getCacheConfig())
if (cacheConfigProvider.getCacheConfig().enableCustomCacheInterceptor) {
builder.addNetworkInterceptor(cacheHeaderInterceptor)
}
return builder.build()
}
}
< /code>
// Implementation with runBlocking in init
@Singleton
class AppCacheConfigProviderImpl @Inject constructor(
private val config: Config,
@ApplicationContext context: Context,
) : AppCacheConfigProvider {
private var sessionCache: Cache?
private var crossSessionCache: Cache?
private var cacheControlConfig: CacheControlConfig
private var appCacheConfig: AppCacheConfig
private var maxAgeUpperBound: Int
private val cacheLock = Any()
init {
// PROBLEM: blocks startup on main thread
runBlocking {
val cacheConfig = preloadConfig.getCacheControlConfig()
crossSessionCache = if (cacheConfig.diskCacheMaxSizeMb > 0) {
Cache(
File(context.cacheDir, cacheConfig.diskCacheFolderName),
(cacheConfig.diskCacheMaxSizeMb * 1024 * 1024).toLong(),
)
} else null
val cacheSize =
(cacheConfig.inMemCacheMaxSizeMb.coerceAtMost(
context.getAvailableMemory() * cacheConfig.inMemCachePercent / 100
)) * 1024 * 1024
sessionCache = if (cacheSize > 0) {
Cache(File(context.cacheDir, "session"), cacheSize.toLong())
} else null
CoroutineScope(Dispatchers.IO).launch {
synchronized(cacheLock) {
try {
sessionCache?.evictAll()
} catch (e: Exception) {}
}
}
cacheControlConfig = config.getCacheControlConfig()
maxAgeUpperBound = config.getMaxAgeUpperBound()
synchronized(cacheLock) {
appCacheConfig = AppCacheConfig(
crossSessionCache,
sessionCache
)
}
}
}
}
< /code>
- What’s the recommended pattern to make this non-blocking but safe?
- Is there a Hilt-friendly way to provide an async dependency without blocking app start but still letting dependents access a current value?
- Any best practices to avoid blocking the main thread while still ensuring required initialization on app launch?
Подробнее здесь: https://stackoverflow.com/questions/797 ... -without-b
Мобильная версия