0

I'm attempting to implement a LiveData reference to data collected via gps and bluetooth, that is also grouped by a foreign key. I don't understand why the Transformations.switchMap doesn't trigger once I create a new foreignkey.

I've moved both, the observer and foreignkey creation around, the LiveData always returns null.

Both, the Database and the Repository are Singletons.

Activity.java

public class Activity extends AppCompatActivity {
    ...
    private ViewModel mViewModel;

    @Override
    onCreate {
        ...
        mViewModel = ViewModelProviders.of( this ).get( ViewModel.class );

        init();
    }

    private class ObserverManager {

        private List<Observer> observers = new ArrayList<>();
        private List<LiveData> livedata = new ArrayList<>();

        public void registerObserver( TextView view, int hid, int uid ) {

            Observer observer = null;
            LiveData ld = null;


            ld = mViewModel.getLatestData();

            observer = ( Observer<Float> ) ( @Nullable final Float data ) -> {

                // String formatting...

            };

            observers.add( observer );
            livedata.add( ld );

            ld.observeForever( observer );

        }

        public void logValue( int index ) {

            Log.d( "OBSERVERMANAGER", String.valueOf( livedata.get( index ).getValue() ) );

        }

    }

    final ObserverManager mObserverManager = new ObserverManager();
}

During init() The foreignkey is inserted and updated, then the observer is attached dynamically.

The service logs the correct foreignkey and inserts values to eData entity, but the Transformations.swapMap never updates, or shows a value other than null.

ViewModel.java

...
private LiveData<Integer> mLiveActivityId;
private LiveData<Float> mLatestData;

ViewModel( Application application ) {
    ...
    mLiveActivityId = mRepository.getLiveActivityId();
    mLatestData = mRepository.getLatestData();
}

public LiveData<Float> getLatestData() {
    return mLatestData;
}

Repository.java

...
private LiveData<Integer> mLiveActivityId;
private LiveData<Float> mLatestData;

Repository( Application application ) {
    ...
    mLiveActivityId = mDataDao.getLiveActivityId();
    mLatestData = Transformations.switchMap( mLiveActivityId, aid -> mDataDao.getLatestData( aid, 0 ) );
}

...
LiveData<Float> getSpeedGPSLatest() {
    return mSpeedGPSLatest;
}

DataDao.java

@Transaction
@Query( "SELECT id FROM eActivity WHERE active = 1" )
LiveData<Integer> getLiveActivityId();

@Transaction
@Query( "SELECT data FROM eData WHERE aid = :aid AND source = :source AND time = " +
            "( SELECT MAX(time) time FROM eData WHERE aid = :aid AND source = :source )" )
LiveData<Float> getLatestData( int aid, int source );

Is it even possible apply Transformations in the repository? So far I have only seen examples with them applied in the ViewModel. Unfortunately, due to some data sharing the same entity with a type field, that would mean I have to pass the LiveData objects back from a function in the repository, which I thought is wrong.

I have also read that switchMap creates a new LiveData object, would this somehow affect how ViewModel cannot read the new object in the repository?

If more code is required to understand the problem, just ask. Am totally stumped on this.

Flecibel
  • 166
  • 2
  • 11
  • You have to observe a `LiveData` to get callbacks. Where do you observe `mLatestData`? – ianhanniballake Aug 12 '19 at 17:05
  • Updated the question to show how I attach observers, can confirm that livedata[] holds livedata, and that observers are attached, but they always return null. – Flecibel Aug 12 '19 at 18:10
  • I'm starting to think that putting an inner query into the where clause results in livedata not updating. Going to try inner join tomorrow. – Flecibel Aug 12 '19 at 19:02
  • See updated Question here: https://stackoverflow.com/questions/57495102/insert-to-room-in-service-not-updating-livedata-in-activity – Flecibel Aug 14 '19 at 12:52

1 Answers1

0

Simply put, yes you can. The Transformations.switchMap() was not the issue with LiveData not updating. Still cannot figure out how to communicate to the room db from a service, ie the follow up question Insert to room in service not updating LiveData in activity, but also solved that by doing things differently.

Flecibel
  • 166
  • 2
  • 11