Marco Jacobo
Marco Jacobo

Reputation: 31

Is it possible to have LIVEDATA observer that trigger only when specific columns changes?

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

Upvotes: 0

Views: 68

Answers (0)

Related Questions