Lorne
Lorne

Reputation: 99

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:

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?

Upvotes: 0

Views: 1311

Answers (2)

Amir Ebrahimi
Amir Ebrahimi

Reputation: 89

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.

Upvotes: 1

Lorne
Lorne

Reputation: 99

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));

Upvotes: 0

Related Questions