简体   繁体   English

我如何等待数据库调用完成

[英]how do I wait for database call to finish

I have the following code in my Android app that reads data from a local SQL database into some variables:我的 Android 应用程序中有以下代码,该应用程序将本地 SQL 数据库中的数据读取到一些变量中:

Context context = getContext();
localSettingsManager = new LocalSettingsManager(context);

LocalDatabase localDatabase = Room.databaseBuilder(context, LocalDatabase.class, context.getResources().getString(R.string.database_name))
                .addMigrations(MIGRATION_1_2)
                .fallbackToDestructiveMigration()
                .build();

DatabaseDAO databaseDAO = localDatabase.DatabaseDAO();
Observable.just(databaseDAO)
                .subscribeOn(Schedulers.io())
                .subscribe(dao -> {
                    Device device = dao.getByCid(localSettingsManager.getDeviceCid());
                    if(device!=null) {
                        currentSerialNumberString = device.serialNumber;
                        currentProxyConfigUrlString = device.proxyConfigURL;
                        currentIMEI1String = device.imei1;
                        currentCarrier1String = device.imei1Carrier;
                        currentIMEI2String = device.imei2;
                        currentCarrier2String = device.imei2Carrier;
                        currentBrandString = device.brand;
                        currentManufacturerString = device.manufacturer;
                        currentModelString = device.model;
                        currentMarketString = device.marketPlaceCID;
                    }

                });
    // wait for data to be read from database before accessing variables

The problem I am having is that when I access the variables that are assigned the data from the database immediately after the database call, they will be read before they are assigned.我遇到的问题是,当我在数据库调用后立即访问从数据库分配数据的变量时,将在分配之前读取它们。 While I can 'fix' the problem by putting in a half second delay, this doesn't feel right.虽然我可以通过延迟半秒来“解决”问题,但这感觉不对。 What is the right way to wait for the database read to finish before continuing program execution?在继续程序执行之前等待数据库读取完成的正确方法是什么?

There are two ways,有两种方式,

  1. do not use threading, that`s mean you will use the main thread to use database and after that continue the other code不要使用线程,这意味着您将使用主线程来使用数据库,然后继续其他代码

  2. in rx-java after that is completed you can use a LiveData Or MutableLiveData and observe it on onCreate method and there are the place you will write other code.完成后在 rx-java 中,您可以使用 LiveData 或 MutableLiveData 并在 onCreate 方法上观察它,您将在此处编写其他代码。

Well, I guess one way is to take away the call to subscribe:好吧,我想一种方法是取消订阅电话:

Context context = getContext();
        localSettingsManager = new LocalSettingsManager(context);

        LocalDatabase localDatabase = Room.databaseBuilder(context, LocalDatabase.class, context.getResources().getString(R.string.database_name))
                .addMigrations(MIGRATION_1_2)
                .fallbackToDestructiveMigration()
                .build();

        DatabaseDAO databaseDAO = localDatabase.DatabaseDAO();

        Device device = databaseDAO.getByCid(localSettingsManager.getDeviceCid());
        if (device != null) {
            currentSerialNumberString = device.serialNumber;
            currentProxyConfigUrlString = device.proxyConfigURL;
            currentIMEI1String = device.imei1;
            currentCarrier1String = device.imei1Carrier;
            currentIMEI2String = device.imei2;
            currentCarrier2String = device.imei2Carrier;
            currentBrandString = device.brand;
            currentManufacturerString = device.manufacturer;
            currentModelString = device.model;
            currentMarketString = device.marketPlaceCID;
        }

        doneButton = view.findViewById(R.id.btnDone);
        doneButton.setOnClickListener(this);
        currentMarket = view.findViewById(R.id.currentMarket);
        currentMarket.setText(String.valueOf(currentMarketString));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM