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 );
7
  • Is it possible to have LIVEDATA observer that trigger only when specific columns changes? No, you cannot do this Commented Jul 20, 2023 at 12:54
  • Why there are 3 fav columns ? Just move fav marking to different table ... Commented Jul 20, 2023 at 12:57
  • Does this answer your question? LiveData update on object field change Commented Jul 20, 2023 at 15:35
  • @Kirguduck no, OP doesn't own LiveData is a part of room implementation it is returned via generated code Commented Jul 20, 2023 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 Commented Jul 21, 2023 at 11:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.