Okey i came with a solution;
this is example entity,
@Entity
data class User(
@PrimaryKey val uid : Int,
val name : String,
val createdTime : Long = System.currentTimeMillis()
)
and below is example json taken from https://jsonplaceholder.typicode.com
and reformated.
private val userJson = "[{\"uid\": 1,\"name\": \"Leanne Graham\"},{\"uid\": 2,\"name\": \" Graham\"},{\"uid\": 3,\"name\": \"Y Graham\"},{\"uid\": 4,\"name\": \"Lea\"}]"
by using https://github.com/FasterXML/jackson-module-kotlin you can deserialize json formatted data easily into data classes in kotlin. Differently from gson library, jakson supports default values so you can set a created time which is current milliseconds. And below is UserDao
@Dao
interface UserDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertUser(user : User)
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertUsers(user : List<User>)
@Query("SELECT * FROM user")
fun getUsers() : List<User>
@Delete()
fun deleteOldUsers(users : List<User>)
than you can get all users with getUser() method and filter them as old and fresh. Than you should delete old ones from database. And if freshUsers is not empty use it, if is empty make a new network request.
val expireTime = TimeUnit.HOURS.toMillis(1)
val userList = App.database.userDao().getUsers()
val freshUsers = userList.filter { it.createdTime > System.currentTimeMillis() - expireTime}
val oldUsers = userList.filter { it.createdTime < System.currentTimeMillis() - expireTime}
App.database.userDao().deleteOldUsers(oldUsers)
This solution working for me. But i'm glad to listen your solutions too.