executor在android体系结构组件中的用法 [英] Usage of executor in android architecture components

查看:93
本文介绍了executor在android体系结构组件中的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

@Inject
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());
        }
    });
}
}

以上代码是Android的应用架构指南" 的一部分使用架构组件的文档.在 refreshUser 方法内,如果缓存中不存在数据,他们将使用改造从网络中获取数据.

The code above is part of "Guide to app architecture", from Android Documentation, using architecture components. Inside refreshUser method they fetch data from network using retrofit if data doesn't exists in cache.

我的问题是:为什么他们为此使用执行器?改造本身已经能够异步运行网络请求.

My question is: Why do they use an executor for this? Retrofit itself is already capable of running network request asynchronously.

在我的示例中,请说明究竟是什么执行器及其需求.

Please clarify what exactly is an Executor and its need in this example for me.

推荐答案

开箱即用的空间不支持主线程上的数据库访问,因此执行程序可以确保在单独的线程上完成工作.

Out of the box room doesn't support database access on the main thread so the executor is there to ensure the work is done on a separate thread.

通过使用执行程序,他们还选择使用同步改造调用,这将阻塞正在执行的线程.

By using the executor they've also opted to use the synchronous retrofit call which will block the executing thread.

在您所引用的执行程序的代码中是SingleThreadExecutor,这实际上创建了一个工作线程来执行其工作,在这种情况下,该线程将执行Room DB操作并处理同步改造调用.

In the code you're referencing the executor is a SingleThreadExecutor, this essentially creates a single worker thread to execute its work, which in this case will execute the Room DB operations along with handling the synchronous retrofit call.

在上面的示例中,这是到执行者的链接.

This is the link the to executor in the above example. https://github.com/PhilippeBoisney/GithubArchitectureComponents/blob/98e0a048791f18646c730323c242c670c3eaf453/app/src/main/java/com/boisneyphilippe/githubarchitecturecomponents/di/module/AppModule.java#L48

有关newSingleThreadExecutor的官方文档: https://docs.oracle .com/javase/7/docs/api/java/util/concurrent/Executors.html#newSingleThreadExecutor()

Along with the official docs for newSingleThreadExecutor: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html#newSingleThreadExecutor()

这篇关于executor在android体系结构组件中的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆