0

Thanks in advance for any advice. I have a query from a roomdatabase, only some columns are exposed by the interface to the user (~ 3 out of 7). The query does not include favorite columns.

So, there is another query to let the user mark the data of a single row as favorite or not favorite...

The issue is the first query is redone every time the user check or uncheck the favorite flag that belongs to the second query. The point here is the first query is livedata that doesnt include the fav columns in the database. So,...

I do not understand why and /I do not know how to resolve it. Any help, would be very appreciated. Thanks!

I have this query in DAO

    @Query( " SELECT   "+COLUMN_NUM+
            " ,        "+COLUMN_TITLE+
            " ,        "+COLUMN_TIME+
            " FROM     "+TABLE_NAME+
            " WHERE    "+" ( :fav1 IS NULL OR "   + COLUMN_FAV1 + " =   :fav1 ) AND "+
                         " ( :fav2 IS NULL OR "   + COLUMN_FAV2 + " =   :fav2 ) AND "+
                         " ( :fav3 IS NULL OR "   + COLUMN_FAV3 + " =   :fav3 ) "    +
            " ORDER BY "+"RANDOM() LIMIT :num"    )
    LiveData< List<Entity_Random> > randomHymns( Integer num, Integer fav1, Integer fav2, Integer fav3);

I have this in view model

final private LiveData<List<Entity_Random>>  mData;

public LiveData<List<Entity_Random>> getData()
{  return Transformations.distinctUntilChanged(mData); }

mData_Trigger.setValue ( new xFilter(7, null, null, null )  );
mData Transformations.switchMap( mData_Trigger,   input -> Dao.randomHymns ( input.num,        input.fav1, input.fav2, input.fav3)  );

I have this in fragment

    mObserver = new Observer<List<Entity_Random>>()
    {   @Override public void onChanged(List<Entity_Random> entity)
        {   mAdapter.swapList(entity);
            mViewModel.updateSingle( mAdapter.getNumber(0));
        }
   };

   mViewModel.getData().observe( getViewLifecycleOwner(),  mObserver );
  • *Is it possible to have LIVEDATA observer that trigger only when specific columns changes?* No, you cannot do this – Selvin Jul 20 '23 at 12:54
  • Why there are 3 fav columns ? Just move fav marking to different table ... – Selvin Jul 20 '23 at 12:57
  • Does this answer your question? [LiveData update on object field change](https://stackoverflow.com/questions/48020377/livedata-update-on-object-field-change) – Kirguduck Jul 20 '23 at 15:35
  • @Kirguduck no, OP doesn't own LiveData is a part of room implementation it is returned via generated code – Selvin Jul 20 '23 at 16:29
  • Thanks @Selvin, I like the idea of making a different table for the fav columns. I'm gona give a try to this. Will let you know the results. Regards – Marco Jacobo Jul 21 '23 at 11:07
  • Moving fav to a different table doesn't make any change,... the query still depends on data from both tables,... so, it triggers again in changes to the child table. – Marco Jacobo Jul 30 '23 at 06:04
  • @Kirguduk, thanks for the recommendation but this is a different issue. – Marco Jacobo Jul 30 '23 at 06:05

0 Answers0