'Can I return the if statement in a Repository for in-memory cache?
so I decided to use this documentation about he in-memory cache for the repository pattern here, without using the Coroutines in the first place. For what I have done I created a response from the server to make a query, however I want this data to be in cache. I've done as the documentation is explained, but however once I put the Boolean parameter, it just won't return anything, so I thought it's going to be the return type, but it says that it found a unit within the if-else statement. My objective is I want to return the cache if there is any data in it, if it doesn't have any data, to call from the server the latest data and write it in the cache and then return it to the application. Any ideas or hints would do nicely, if there is something that it didn't made sense of my question, feel free to correct me about it. Thank you in advance.
The DTO Model:
package com.example.spaceflightnews.network.model
data class ArticleResource(
val title: String,
val imageUrl: String,
val summary: String,
val publishedAt: String,
)
DataSource:
package com.example.spaceflightnews.network.datasource
import android.util.Log
import com.example.spaceflightnews.network.Retrofit
import com.example.spaceflightnews.network.model.ArticleResource
import com.example.spaceflightnews.network.spaceflightNewsAPI.ArticlesAPI
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class ArticleNetworkDataSource(
private val serviceApi: ArticlesAPI = Retrofit().getArticleInstance().create(ArticlesAPI::class.java)
) {
//Articles model call and callback
fun getArticleRequest(
callback: DataReadyCallback
) {
val call: Call<List<ArticleResource>> = serviceApi.getArticles()
call.enqueue(object : Callback<List<ArticleResource>> {
override fun onResponse(
call: Call<List<ArticleResource>>,
response: Response<List<ArticleResource>>
) {
response.body()?.run {
callback.onDataReady(this)
}
}
override fun onFailure(call: Call<List<ArticleResource>>, t: Throwable) {
Log.e("Error", t.localizedMessage!!)
}
})
}
interface DataReadyCallback {
fun onDataReady(data: List<ArticleResource>)
}
}
Repository:
class ArticlesRepository(
private val articlesNetworkDataSource: ArticleNetworkDataSource = ArticleNetworkDataSource()
) {
private var latestArticles: List<ArticleResource>? = null
fun getDataListObject(
mutableLiveData: MutableLiveData<List<ArticleResource>>,
isDataAddedInCache: Boolean = false
) {
if (isDataAddedInCache) {
//Calling the query
articlesNetworkDataSource.getArticleRequest(object : ArticleNetworkDataSource.DataReadyCallback {
override fun onDataReady(data: List<ArticleResource>) {
latestArticles = data
latestArticles?.run {
//Send it to the ViewModel
mutableLiveData.value = this
}
}
})
} else {
//If there is data in it, return that cache so it won't call the query again from the server.
latestArticles?.run {
mutableLiveData.value = this
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
