android-livedata

Android LiveData: Not receiving all notifications

这一生的挚爱 提交于 2019-12-10 18:29:46
问题 I'm experimenting with Android's LiveData. I just tried to push a lot of notifications to an observer that observes a LiveData object. I let a thread run in background and in a while-loop i constantly push random values via LiveData's postValue-method. The number of received notifications (number of onChanged()-callbacks) in the observer which observes the livedata is much less then the number of calls of postValue in the background thread. Can somebody explain what's the reason for this?

Failing to load next data with android Paging library

随声附和 提交于 2019-12-10 17:19:33
问题 I am trying to show call log list using Room-Paging-LiveData-ViewModel. Without paging my code works perfectly. And I want to use paging also. In my database I have total 25 call log record. The first 9 call log is showing in the list. By debugging I found that while reading data in view model via Dao , it is returning list of size 25. But only first 9 of these are non null. All other entries in the list is null. I am expecting the null data will refresh soon as this is a paged list. But the

LiveData is abstract android

我是研究僧i 提交于 2019-12-10 15:52:53
问题 I tried initializing my LiveData object and it gives the error: "LiveData is abstract, It cannot be instantiated" LiveData listLiveData = new LiveData<>(); 回答1: Since it is abstract (as @CommonsWare says) you need to extend it to a subclass and then override the methods as required in the form: public class LiveDataSubClass extends LiveData<Location> { } See docs for more details 回答2: In a ViewModel, you may want to use MutableLiveData instead. E.g.: class MyViewModel extends ViewModel {

Android Repository pattern

与世无争的帅哥 提交于 2019-12-10 14:04:15
问题 I have couple of questions about Repository pattern: If I'm using only offline database for example Room with LiveData is there any use of Repository pattern? If my app is offline right now, but will be connected to remote db in the future should I implement repository pattern or it's not going to be a problem to do it later? 回答1: To begin with, Repository pattern have nothing to do with technology or programming language. Repository pattern is useful to separate persistence concerns from

Android MVVM - How to make LiveData emits the data it has (forcing to trigger the observer)

喜夏-厌秋 提交于 2019-12-10 13:33:49
问题 I have this ViewModel that gets a list from the network and I populate a RecyclerView with the data ( MyAvailabilityRepository returns a MutableLiveData , that's why i'm using Transformations.switchMap ): class MyAvailabilityViewModel : ViewModel() { private val getListsParams = MutableLiveData<String>() private val getListsObservable = Transformations.switchMap(getListsParams) { organizationId -> MyAvailabilityRepository.getSectionedLists(organizationId) } fun getListsObservable() : LiveData

Update viewmodel from a different class

北慕城南 提交于 2019-12-10 11:57:25
问题 I am trying to understand ViewModel and LiveData. In MainActivity, I am observing LiveData In MyTask, I am setting data on the LiveData, that should be displayed in the activity. Problem is data set in MyTask is not getting updated on the UI. MainActivity public class MainActivity extends AppCompatActivity { private MyViewModel viewModel; private TextView tv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity

How to convert a List<Object> to PagedList<Object> and vice-versa?

非 Y 不嫁゛ 提交于 2019-12-10 01:02:35
问题 PagedList<Object> is used for Android's cool paging library. To make the question as minimal as possible : If i have a list of strings like List<String> stringList; // it consists of 200 strings I want to convert stringList to type PagedList<String> like PagedList<String> pagedStringList; And also if i have a PagedList<Object> how can convert it to a List<Object> ? I went through this for reference If i try the other way round .... How can I convert List<Object> into DataSource.Factory

Shared ViewModel to help communication between fragments and parent activity

我的梦境 提交于 2019-12-09 06:29:20
问题 While Navigation component of JetPack looks pretty promising I got to a place where I could not find a way to implement something I wanted. Let's take a look at a sample app screen: The app has one main activity, a top toolbar, a bottom toolbar with fab attached. There are 2 challenges that I am facing and I want to make them the right way. 1. I need to implement fragment transactions in order to allow replacing the fragment on the screen, based on the user interaction. There are three ways I

How can I selectively observe Android Room database INSERT, DELETE, and UPDATE events?

前提是你 提交于 2019-12-08 11:26:26
问题 I have DAO designed this way. @Dao interface FooDao { @Query("SELECT * FROM foo") LiveData<List<Foo>> stream(); } The above lets me receive INSERT , UPDATE , and DELETE events all at once, which I have no way of knowing whether the data is recently inserted, deleted, or being updated. Goal My goal is to be able to update the database rapidly and have RecyclerView reflect the changes without stuttering or completely freezing. Current situation I have a background service that's updating the

Getting Initial Value for LiveData Always Returning Null

旧街凉风 提交于 2019-12-08 00:58:37
问题 I am trying to load the loggedInUser from the Local Room Database, when the App starts. I would like to skip prompting user to log-in if the saved Authentication Token of the previously saved user is still valid! So, from the DAO , I want to return a LiveData object containing the previously logged-in user , then observe it for subsequent changes. The challenge I have is that the method to get the currently logged-in user always returns null if I wrap the result inside a LiveData , but it