Search code examples
androidbroadcastreceiverandroid-roomandroid-livedata

How to access data from a Roomdatabase inside a BroadcastReceiver class


I need to access data from my Room database inside a BroadCastReceiver class, but as you know we need a lifecycle owner to get an instance of ViewModel class as shown below.

public class AlertReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationHelper.sendFinanceLoggingNotification(context);
        RecurrenceInfoViewModel recurrenceInfoViewModel = new ViewModelProvider(this).get(RecurrenceInfoViewModel.class);

    }
}

when passing "this" as the lifecycle owner android studio is throwing error. Can anyone please guide me from where I can get a lifecycle owner inside a BroadCastReceiver or if you can suggest any other way of accessing the data. Below are my ViewModel and Repository classes

    public class RecurrenceInfoViewModel extends AndroidViewModel {


    private LiveData<List<RecurrenceInfoEntity>> allRecurrenceInfos;
    private RecurrenceInfoRepository recurrenceInfoRepository;

    public RecurrenceInfoViewModel(@NonNull Application application) {
        super(application);
        recurrenceInfoRepository=new RecurrenceInfoRepository(application);


    }

    public void insertRecurrenceInfo(RecurrenceInfoEntity recurrenceInfoEntity) {
        recurrenceInfoRepository.insertRecurrenceInfo(recurrenceInfoEntity);
    }

    public void updateRecurrenceInfo(RecurrenceInfoEntity recurrenceInfoEntity) {

        recurrenceInfoRepository.updateRecurrenceInfo(recurrenceInfoEntity);
    }

    public void deleteRecurrenceInfo(RecurrenceInfoEntity recurrenceInfoEntity) {
        recurrenceInfoRepository.deleteRecurrenceInfo(recurrenceInfoEntity);
    }

    public void deleteAllRecurrenceInfos() {
        recurrenceInfoRepository.deleteAllRecurrenceInfo();
    }



    public LiveData<RecurrenceInfoEntity> getAllRecurrenceInfos(String recurrenceInfoKey) {
        return recurrenceInfoRepository.getRecurrenceInfoEntityList(recurrenceInfoKey);
    }
}





public class RecurrenceInfoRepository {


    private RecurrenceInfoDao recurrenceInfoEntityDao;
    private LiveData<List<RecurrenceInfoEntity>> recurrenceInfoEntityList;

    public RecurrenceInfoRepository(Context context) {

        MoneyManagerDatabase moneyManagerDatabase = MoneyManagerDatabase.getInstance(context);
        recurrenceInfoEntityDao = moneyManagerDatabase.getRecurrenceInfoDao();
        recurrenceInfoEntityList = recurrenceInfoEntityDao.getAllRecurrenceInfo();


    }

    public void insertRecurrenceInfo(RecurrenceInfoEntity data) {

        new PerformSingleColumnDataOperations(recurrenceInfoEntityDao,
                Constants.INSERT_SINGLE_NODE_DATABASE_OPERATION).execute(data);
    }

    public void updateRecurrenceInfo(RecurrenceInfoEntity data) {
        new PerformSingleColumnDataOperations(recurrenceInfoEntityDao,
                Constants.UPDATE_SINGLE_NODE_DATABASE_OPERATION).execute(data);
    }

    public void deleteRecurrenceInfo(RecurrenceInfoEntity data) {
        new PerformSingleColumnDataOperations(recurrenceInfoEntityDao,
                Constants.DELETE_SINGLE_NODE_DATABASE_OPERATION).execute(data);
    }

    public void deleteRecurrenceInfo(String  type) {
        new PerformSingleColumnDataOperations(recurrenceInfoEntityDao,
                Constants.DELETE_SINGLE_NODE_DATABASE_OPERATION).execute();
    }

    public void deleteAllRecurrenceInfo() {
        new PerformSingleColumnDataOperations(recurrenceInfoEntityDao,
                Constants.DELETE_ALL_NODES_DATABASE_OPERATION).execute();
    }

    public LiveData<RecurrenceInfoEntity> getRecurrenceInfoEntityList(String key) {
        return recurrenceInfoEntityDao.getAllRecurrenceInfo(key);
    }


    private static class PerformSingleColumnDataOperations extends AsyncTask<RecurrenceInfoEntity, Void, Void> {

        private RecurrenceInfoDao dataDao;
        private String operationType;


        PerformSingleColumnDataOperations(RecurrenceInfoDao dataDao, String operationType) {
            this.dataDao = dataDao;
            this.operationType = operationType;

        }

        @Override
        protected Void doInBackground(RecurrenceInfoEntity... recurrenceInfoEntities) {
            switch (operationType) {
                case Constants.INSERT_SINGLE_NODE_DATABASE_OPERATION:
                    dataDao.insertRecurrenceInfo(recurrenceInfoEntities[0]);
                    break;
                case Constants.UPDATE_SINGLE_NODE_DATABASE_OPERATION:
                    dataDao.updateRecurrenceInfo(recurrenceInfoEntities[0]);
                    break;
                case Constants.DELETE_SINGLE_NODE_DATABASE_OPERATION:
                    dataDao.deleteRecurrenceInfo(recurrenceInfoEntities[0]);
                    break;
                case Constants.DELETE_ALL_NODES_DATABASE_OPERATION:
                    dataDao.deleteAllRecurrenceInfo();
            }

            return null;
        }
    }




}

Thanks in advance.


Solution

  • I have solved the above problem by NOT using LiveData. You can access data from Room anywhere by just providing the ApplicationContext as shown below.

    DAO:

    @Query("SELECT * FROM reference_info where recurrenceInfoPrimaryKey=:recurrenceinfoprimkey")
        RecurrenceInfoEntity getAllRecurrenceInfoWithOutLiveData(String recurrenceinfoprimkey);
    

    Repository:

      public RecurrenceInfoEntity getRecurrenceInfoEntityWithOutLiveData(String key) {
            return recurrenceInfoEntityDao.getAllRecurrenceInfoWithOutLiveData(key);
        }
    

    BroadCastReceiver:

        public class AlertReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
    new Thread(() -> {          
          RecurrenceInfoEntity recurrenceInfoEntity = 
         recurrenceInfoRepository.getRecurrenceInfoEntityWithOutLiveData(Constants.LOG_FINANCES_RECURRENCE_KEY);
    
             }).start();
            }