LiveData不会在打开应用程序时立即显示数据

问题描述 投票:1回答:1

The Issue

我实现了Android架构库,我正在使用MVVM(模型视图 - 模型)从Room数据库恢复数据,每当我使用观察者查看LiveDada时,每次启动应用程序时都会有明显的延迟回收者视图加载。

图片说明了我的意思,当应用统计数据和项目加载时显示延迟。

What I want it to do

我想用LiveData实现的是this,我能够实现这一点的方式是在我的Dao中我使用Query获取所有数据并将其作为List传递而不是使用LiveData然后在Repository中将其转换为MutableLiveData然后将它传递给数据库并从那里观察它作为我的片段中的LiveData但是使用这种方法实际上不会在删除或插入时更新,除非我重新启动应用程序。 有什么方法可以解决这个问题吗? 我非常想使用LiveData。

这是我的DevicesDao界面:

@Dao
public interface DevicesDao {

   @Insert
   void insert(Devices... devices);

   @Query("SELECT * FROM devices")
   LiveData<List<Devices>> getDevices();
   /*
   @Query("SELECT * FROM devices")
   List<Devices> getDevices();
   */
   @Delete
    void delete(Devices... device);

    @Update
    void update(Devices... device);
}

数据库:

@Database(entities = {Devices.class}, version = 1)
public abstract class DevicesDatabase extends RoomDatabase {

    private static final String DATABASE_NAME = "devices_registered";

    private static DevicesDatabase instance;

    public abstract DevicesDao devicesDao();

    public static DevicesDatabase getInstance(final Context context) {
        if (instance == null) {
            synchronized (DevicesDatabase.class) {
                if (instance == null) {
                    instance = Room.databaseBuilder(
                            context.getApplicationContext(),
                            DevicesDatabase.class,
                            DATABASE_NAME)
                            .fallbackToDestructiveMigration()
                            .build();
                }
            }
        }
        return instance;
    }
}

库:

public class DevicesRepository {

    private final DevicesDao devicesDao;

    public DevicesRepository(Application application) {
        DevicesDatabase db = DevicesDatabase.getInstance(application);
        devicesDao = db.devicesDao();
    }

    public void addDevices(Devices devices) {
        new InsertDeviceAsync(devicesDao).execute(devices);
    }

    public void updateDevice(Devices devices) {
        new UpdateDeviceAsync(devicesDao).execute(devices);
    }

    public void deleteDevice(Devices devices) {
        new DeleteDeviceAsync(devicesDao).execute(devices);
    }

    //Gets all data from SQLite
    public LiveData<List<Devices>> getAllDevices() {
        return devicesDao.getDevices();
    }

    /*
    public LiveData<List<Devices>> getAllDevices() {
        MutableLiveData<List<Devices>> devices = new MutableLiveData<>();
        try {
            devices.setValue(new GetDeviceAsync(devicesDao).execute().get());
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return devices;
    }
    */
}

查看模型文件:

public class HomeFragmentViewModel extends AndroidViewModel {

    private final DevicesRepository devicesRepository;
    private LiveData<List<Devices>> devices;

    public HomeFragmentViewModel(@NotNull Application application) {
        super(application);
        devicesRepository = new DevicesRepository(application);
        devices = devicesRepository.getAllDevices();
    }


    public LiveData<List<Devices>> getAllDevices() {
        return devices;
    }

    public void addNewDevice(Devices devices) {
        devicesRepository.addDevices(devices);
    }

    public void deleteDevice(Devices devices) {
        devicesRepository.deleteDevice(devices);
    }

    public void editDevice(Devices devices) {
        devicesRepository.updateDevice(devices);
    }
}

最后,我片段中的观察者:

   ///////Other code
    //Implements ViewModel to HomeFragment
    homeFragmentViewModel = ViewModelProviders.of(this).get(HomeFragmentViewModel.class);

   homeFragmentViewModel.getAllDevices().observe(getViewLifecycleOwner(), devicesList -> {

        //Validation tool
        validationUtil = new ValidationUtil(devicesList);

        //Adds to adapter
        adapter.submitList(devicesList);

       /////// Other code
    });

感谢您的时间!!!

android architecture android-livedata
1个回答
0
投票

新更新:当我重置应用程序数据时,它会正常加载,但是一旦我决定更新代码,无论我在Android Studio中进行哪种编辑,问题都会回来。

© www.soinside.com 2019 - 2024. All rights reserved.