android - 将当前值与架构组件中的最新值进行比较

标签 android android-room android-architecture-components

我从 api 得到了一个复杂的 json,我用 Architect component(Room) 保存了这些值。

问题:

  • 我能否将当前值与我在 SQlite 中保存的上一个值进行比较,如果在比较中发现差异,则更新 RecyclerView
  • 这种方法合乎逻辑吗?
  • 您有更好的方式提供吗?
  • 如果您有更好的方式提供给我一个示例(url sample)

最佳答案

是的,你可以这样做,这实际上是推荐的方式。为此,我认为您应该利用 Android Jetpack 引入的另外两个架构组件,而不仅仅是 Room 数据库:ViewModelLiveData ,但这不是强制性的。

重要的是为您的应用程序添加一个名为Repository 的额外层:

Repository modules handle data operations. They provide a clean API so that the rest of the app can retrieve this data easily. They know where to get the data from and what API calls to make when data is updated. You can consider repositories to be mediators between different data sources, such as persistent models, web services, and caches.

所以基本上,处理此问题的建议架构如下所示:

Android recommended architecture

考虑到这一点,从网络服务检索用户数据并将其保存到本地房间数据库的存储库示例如下所示:

public class UserRepository {
    private final Webservice webservice;
    private final UserDao userDao;
    private final Executor executor;

    public UserRepository(Webservice webservice, UserDao userDao, Executor executor) {
        this.webservice = webservice;
        this.userDao = userDao;
        this.executor = executor;
    }

    public LiveData<User> getUser(String userId) {
        refreshUser(userId);
        // Returns a LiveData object directly from the database.
        return userDao.load(userId);
    }

    private void refreshUser(final String userId) {
        // Runs in a background thread.
        executor.execute(() -> {
            // Check if user data was fetched recently.
            boolean userExists = userDao.hasUser(FRESH_TIMEOUT);
            if (!userExists) {
                // Refreshes the data.
                Response<User> response = webservice.getUser(userId).execute();

                // Check for errors here.

                // Updates the database. The LiveData object automatically
                // refreshes, so we don't need to do anything else here.
                userDao.save(response.body());
            }
        });
    }
}

然后,您的 ViewModel 将获取用户实时数据,执行如下操作:

...
user = userRepo.getUser(userId);
...

并且它将使用公共(public)方法将该 LiveData 提供给 UI 层:

...
public LiveData<User> getUser() {
        return this.user;
}
...

最后,您可以从您的 UI 层(Activity 或 Fragment)观察ViewModel 中的 LiveData 并相应地调整 UI。

viewModel.getUser().observe(this, user -> {
      // Update UI.
});

要获得更完整的解释,我建议您查看:

  1. Guide to app architecture in Android`s Developers website.
  2. This Github project with a basic example.
  3. This other Github project with a more complex example.

关于android - 将当前值与架构组件中的最新值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53895716/

相关文章:

android - 更改 android numberpicker 分隔线颜色

android - 将 ArrayList 从 Activity 传递到 ListFragment?

android - 表间关系问题

android - Kotlin 1.2.40 版本上的 Kotlin 房间数据库崩溃

安卓喷气背包 : lifecycle-aware recurrent periodic task execution with LiveData and ViewModels

android - 注释参数必须是编译时常量

android - Android 上的虚拟蓝牙设备?

Android 将 WebView 复制到 Webview

android - 在Spinner中使用LiveData

android - WorkerManager 是否是在应用程序中实现警报/提醒功能的可靠方式?