【问题标题】:how do I wait for database call to finish我如何等待数据库调用完成
【发布时间】:2021-08-21 11:19:56
【问题描述】:

我的 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

我遇到的问题是,当我在数据库调用后立即访问从数据库分配数据的变量时,它们将在分配之前被读取。虽然我可以通过延迟半秒来“解决”问题,但这感觉不对。在继续程序执行之前等待数据库读取完成的正确方法是什么?

【问题讨论】:

标签: java android database multithreading


【解决方案1】:

有两种方法,

  1. 不要使用线程,这意味着你将使用主线程来使用数据库,然后继续其他代码

  2. 在 rx-java 完成之后,你可以使用 LiveData 或 MutableLiveData 并在 onCreate 方法上观察它,你将在其中编写其他代码。

【讨论】:

    【解决方案2】:

    好吧,我想一种方法是取消订阅电话:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      相关资源
      最近更新 更多