android - 如何避免不必要的api调用,只有在服务器端发生一些变化时才执行api调用
问题描述
fun getNotifications(priorityNum: Int) =
object : NetworkBoundResource<List<Notification>, Resource<List<Notification>>>(exec) {
override fun saveCallResult(item: Resource<List<Notification>>) {
item.data!!.forEach {
val id = db.notification().insert(it)
if (id == -1L) {
db.notification().update(
it.id,
it.subject,
it.description,
it.priority,
it.community,
it.createdTime
)
}
}
}
override fun shouldFetch(data: List<Notification>?) = true
override fun loadFromDb(): LiveData<List<Notification>> {
return db.notification().getAllNotifications()
}
override fun createCall(): LiveData<ApiResponse<Resource<List<Notification>>>> =
api.getNotifications(pref.userId!!, priorityNum)
}.asLiveData()
我在本地数据库的存储库中使用了类似的东西。我想要的是您可以看到 shouldFetch() 始终为真,即使数据没有更改也会导致 API 调用。因此,这会导致不需要的 API 调用。如何避免这种情况并仅在服务器端有新的插入或更新时才调用后端
解决方案
推荐阅读
- javascript - 当我保存方法的返回时,它保存未定义
- laravel-8 - Doctrine\DBAL\Driver\PDO\Exception SQLSTATE[28000] [1045] Access denied for user 我得到一个错误
- html - 如何为 tbody 中的所有表数据设置一个表头?
- javascript - 如何下载远程图像然后将图像作为图像文件上传以进行表单提交?
- r - 如何在 Rstudio 上的模糊逻辑变量中同时使用多个成员资格(三角形和梯形)?
- kerberos - traefik v2 重定向 TCP 请求
- macros - Doyxgen一次性宏扩展/扩展命令
- jenkins - 尝试使用 Homebrew 安装 jenkins 时出错
- azure-blob-storage - Azure Blob 存储不在本地存储
- c++ - C++ 使用 OpenMP #pragma 和 std:thread?