Android Arch Component Sample简单解析

本文介绍了Android官方的BasicSample,展示了如何使用LifeCycle、ViewModel、Room、LiveData等Arch Component组件。通过源码分析,详细解释了数据加载流程,包括从本地数据库初始化数据,涉及到DataBinding库的使用。在页面生命周期中添加日志,详细梳理了从创建ViewModel、监听LiveData到数据变化影响UI的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BasicSample

这是谷歌官方提供的一个关于Android Architecture Component 库使用的例子,通过查看源码,这个例子只简单的展示如何使用Architecture Component库中的相关构件的使用,包括LifeCycle,ViewModel,Room,LiveData等,其中还涉及了DataBinding这个库。看起来挺简单的,并且不涉及网络请求,只是单纯的对数据库进行初始化,然后加载数据库的相关数据。

接下来我们来看下具体的代码。

首先我们回顾下官方提供的架构:
这里写图片描述

从上图中我们可以看到,首先创建一个页面(Activity/Fragment),然后根据显示的页面创建ViewModel,ViewModel通过LiveData去渲染当前的页面,LiveData通过Repository来初始化数据,如果有本地数据则加载本地数据,如果没有则进行网络请求,通过网络请求获取数据,然后本地化,然后重新加载。这么一个流程下来后,页面就加载完成了。

然后我们在页面的生命周期中加入一些日志:
这里写图片描述

通过代码我们梳理了具体的流程如下:
basic_sample

如何使用生命感知:
一方面是通过数据的观察,一方面是对生命周期的感知,接下来上代码

虽然代码不多,我们也只看关键部分:

ProductListFragment.java

@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d("AAC", "ProductListFragment onActivityCreated");
        final ProductListViewModel viewModel =
                ViewModelProviders.of(this).get(ProductListViewModel.class);

        subscribeUi(viewModel);

    }

这时为对应的页面创建ViewModel。

主要是如何订阅UI,并根据数据变更,刷新UI

private void subscribeUi(ProductListViewModel viewModel) {
        // Update the list when the data changes
        Log.d("AAC", "ProductListFragment subscribeUi");
        viewModel.getProducts().observe(this, new Observer<List<ProductEntity>>() {
            @Override
            public void onChanged(@Nullable List<ProductEntity> myProducts) {
                Log.d("AAC", "onChanged() called with: myProducts = [" + myProducts + "]");
                if (myProducts != null) {
                    mBinding.setIsLoading(false);
                    mProductAdapter.setProductList(myProducts);
                } else {
                    mBinding.setIsLoading(true);
                }
            }
        });
    }

通过对LiveData < List < ProductEntity > >数据的监听,通过观察数据的变化对UI进行刷新。

然后我们就看什么时候数据会变化-》

ProductListViewModel.java

public ProductListViewModel(Application application) {
        super(application);
        Log.d("AAC", "ProductListViewModel()");
        final DatabaseCreator databaseCreator = DatabaseCreator.getInstance(this.getApplication());

        LiveData<Boolean> databaseCreated = databaseCreator.isDatabaseCreated();
        Log.d("AAC", "ProductListViewModel: databaseCreated = " + databaseCreated.getValue());
        mObservableProducts = Transformations.switchMap(databaseCreated,
                new Function<Boolean, LiveData<List<ProductEntity>>>() {
            @Override
            public LiveData<List<ProductEntity>> apply(Boolean isDbCreated) {
                Log.d("AAC", "ProductListViewModel Transformations.switchMap apply() called with: isDbCreated = [" + isDbCreated + "]");
                if (!Boolean.TRUE.equals(isDbCreated)) { // Not needed here, but watch out for null
                    //noinspection unchecked
                    return ABSENT;
                } else {
                    //noinspection ConstantConditions
                    return databaseCreator.getDatabase().productDao().loadAllProducts();
                }
            }
        });
        databaseCreator.createDb(this.getApplication());
        Log.d("AAC", "ProductListViewModel: databaseCreated = " + databaseCreated.getValue());
    }

在ProductListViewModel构造的时候,会通过对LiveData< Boolean > databaseCreated数据进行观察,通过数据的变化来初始化LiveData < List < ProductEntity > >数据。

DatabaseCreator.java

/**
     * Creates or returns a previously-created database.
     * <p>
     * Although this uses an AsyncTask which currently uses a serial executor, it's thread-safe.
     */
    public void createDb(Context context) {
        Log.d("AAC", "DatabaseCreator createDb");
        Log.d("DatabaseCreator", "Creating DB from " + Thread.currentThread().getName());

        if (!mInitializing.compareAndSet(true, false)) {
            return; // Already initializing
        }

        mIsDatabaseCreated.setValue(false);// Trigger an update to show a loading screen.
        new AsyncTask<Context, Void, Void>() {

            @Override
            protected Void doInBackground(Context... params) {
                Log.d("AAC", "DatabaseCreator createDb doInBackground: ");
                Log.d("DatabaseCreator",
                        "Starting bg job " + Thread.currentThread().getName());

                Context context = params[0].getApplicationContext();

                // Reset the database to have new data on every run.
                context.deleteDatabase(DATABASE_NAME);

                // Build the database!
                AppDatabase db = Room.databaseBuilder(context.getApplicationContext(),
                        AppDatabase.class, DATABASE_NAME).build();

                // Add a delay to simulate a long-running operation
                addDelay();

                // Add some data to the database
                DatabaseInitUtil.initializeDb(db);
                Log.d("DatabaseCreator",
                        "DB was populated in thread " + Thread.currentThread().getName());

                mDb = db;
                return null;
            }

            @Override
            protected void onPostExecute(Void ignored) {
                // Now on the main thread, notify observers that the db is created and ready.
                Log.d("AAC", "DatabaseCreator createDb onPostExecute");
                mIsDatabaseCreated.setValue(true);
                Log.d("AAC", "DatabaseCreator createDb onPostExecute mIsDatabaseCreated = " + mIsDatabaseCreated.getValue());
            }
        }.execute(context.getApplicationContext());
    }

我们可以看到是在创建数据库的时候,去设置databaseCreated,通过这个变量可以控制控制是否显示加载中,当数据初始化完成后,再设置为true。

就这样,所有的事件都串起来了,接下来就是核心的如何触发这些事件的代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值