android-livedata

Observe LiveData from foreground service

无人久伴 提交于 2019-11-28 09:14:13
I have a repository which holds the LiveData object and is used by both activity and a foreground service through a ViewModel. When I start observing from the activity everything works as expected. However observing from the service doesn't trigger the Observe. This is the code I use class MyService: LifecycleService() { lateinit var viewModel: PlayerServiceViewModel override fun onCreate() { viewModel = MyViewModel(applicationContext as Application) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { viewModel.getLiveData().observe(this, Observer { data -> // Do

Why there's a separate MutableLiveData subclass of LiveData?

落爺英雄遲暮 提交于 2019-11-28 04:50:23
It looks like MutableLiveData differs from LiveData only by making the setValue() and postValue() methods public, whereas in LiveData they are protected. What are some reasons to make a separate class for this change and not simply define those methods as public in the LiveData itself? Generally speaking, is such a form of inheritance (increasing the visibility of certain methods being the only change) a well-known practice and what are some scenarios where it may be useful (assuming we have access to all the code)? In LiveData - Android Developer Documentation , you can see that for LiveData

Android LiveData prevent receive the last value when subscribe

帅比萌擦擦* 提交于 2019-11-28 00:29:32
Is it possible to prevent LiveData receive the last value when start observing? I am considering to use LiveData as events. For example events like show message, a navigation event or a dialog trigger, similar to EventBus. I found something similar SingleLiveEvent - but it work only for 1 observer and not for multiple observers. I created a new Class that will hold my real data and a "special ID": class LiveDataItem { long mRealtimeNanos; YOUR_PREVIOUS_LIVEDATA_TYPE mData; LiveDataItem(YOUR_PREVIOUS_LIVEDATA_TYPE data, long realtimeNanos) { this.mRealtimeNanos = realtimeNanos; this.mData =

Error:Program type already present: android.arch.lifecycle.LiveData

落花浮王杯 提交于 2019-11-27 22:44:19
When I press the play button in Android Studio, my app compiles but is shows this error (redacted): Error:Program type already present: android.arch.lifecycle.LiveData (Full log) I've tried deleting the .gradle folder, then going to Build > Clean Project and Build > Rebuild Project . However, it doesn't work. I've also tried deleting the source code, then cloning again from git and importing the folder to Android Studio. However, it still produces that error. Here's my app's app/build.gradle : apply plugin: 'com.android.application' android { compileSdkVersion 27 buildToolsVersion '27.0.3'

LiveData.getValue() returns null with Room

社会主义新天地 提交于 2019-11-27 21:13:58
Java POJO Object public class Section { @ColumnInfo(name="section_id") public int mSectionId; @ColumnInfo(name="section_name") public String mSectionName; public int getSectionId() { return mSectionId; } public void setSectionId(int mSectionId) { this.mSectionId = mSectionId; } public String getSectionName() { return mSectionName; } public void setSectionName(String mSectionName) { this.mSectionName = mSectionName; } } My Query method @Query("SELECT * FROM section") LiveData<List<Section>> getAllSections(); Accessing DB final LiveData<List<Section>> sections = mDb.sectionDAO().getAllSections()

Android implement search with view model and live data

北慕城南 提交于 2019-11-27 18:42:43
问题 I'm working on a project in android for a udacity course I'm currently trying to implement a search function while adhering to android architecture components and using firestore and room I'm fairly new to all these concepts so please point out anything that seems wrong. So I made a database repository to keep my firestore and room databases in sync and to deliver the data. I'm then using viewmodel and the observer pattern (I think) so my observer gets the data and looks for changes gives it

Room - LiveData observer does not trigger when database is updated

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 14:41:48
I am trying to find out in the code below, why is it that Room's LiveData observable does not give me new shifts once I populate the database with new data. This is put on my activity's onCreate method: shiftsViewModel = ViewModelProviders.of(this).get(ShiftsViewModel.class); shiftsViewModel .getShifts() .observe(this, this::populateAdapter); This is the populateAdapter method: private void populateAdapter(@NonNull final List<Shift> shifts){ recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(shifts)); } I also have the following code that populates the database (I use RxJava to do the

Best practice for using MediatorLiveData only by present data

一笑奈何 提交于 2019-11-27 07:23:01
问题 What is the best practice for using the MediatorLiveData with multiple sources? I have a MediatorLiveData in the ViewModel, that is accessed from the view for the data, that should finally be presented. The MediatorLiveData depends on multiple other LiveDatas. Some of them come from the repository layer, some of them have to be processed in the ViewModel before they can be accessed from the MediatorLiveData and some of them come from the View. So my current implementation looks like the

setValue and postValue on MutableLiveData in UnitTest

可紊 提交于 2019-11-27 06:07:03
问题 I try to test some methods on my Application but I get an error when calling mutablelivedata.postValue. Here is a snippet and the error message: @Test public void callStartScreenRepository(){ Observer<User> userObserver = mock(Observer.class); startScreenViewModel.returnUser().observeForever(userObserver); maennlich = new User(); maennlich.setVorname("Christian"); MutableLiveData<User> userTest = new MutableLiveData<>(); userTest.postValue(maennlich); when(startScreenRepository.userWebService

Is it possible to make one LiveData of two LiveDatas?

假如想象 提交于 2019-11-27 04:49:00
问题 I have two DAOs, two Repositories and two POJOs. There is some way to create one Livedata of two? I need it to make single list for Recyclerview. POJOs are similar objects. ExpenseRepository: public class ExpenseRepository { private ExpenseDao expenseDao; private LiveData<List<Expense>> allExpenses; public ExpenseRepository(Application application) { ExpenseIncomeDatabase database = ExpenseIncomeDatabase.getInstance(application); expenseDao = database.expenseDao(); allExpenses = expenseDao