Using Realm and LiveData. Converting LiveData<RealmResults<CustomModelObject>> to LiveData<List<Custom
By : zawminoo
Date : March 29 2020, 07:55 AM
Any of those help In your case, replacing LiveData with LiveData> would be enough to solve your problem. However, I'd advise trying out the RealmLiveResults class that is available in the official example: code :
/**
* This class represents a RealmResults wrapped inside a LiveData.
*
* Realm will always keep the RealmResults up-to-date whenever a change occurs on any thread,
* and when that happens, the observer will be notified.
*
* The RealmResults will be observed until it is invalidated - meaning all local Realm instances on this thread are closed.
*
* @param <T> the type of the RealmModel
*/
public class LiveRealmResults<T extends RealmModel> extends LiveData<List<T>> {
private final RealmResults<T> results;
// The listener will notify the observers whenever a change occurs.
// The results are modified in change. This could be expanded to also return the change set in a pair.
private OrderedRealmCollectionChangeListener<RealmResults<T>> listener = new OrderedRealmCollectionChangeListener<RealmResults<T>>() {
@Override
public void onChange(@NonNull RealmResults<T> results, @Nullable OrderedCollectionChangeSet changeSet) {
LiveRealmResults.this.setValue(results);
}
};
@MainThread
public LiveRealmResults(@NonNull RealmResults<T> results) {
//noinspection ConstantConditions
if (results == null) {
throw new IllegalArgumentException("Results cannot be null!");
}
if (!results.isValid()) {
throw new IllegalArgumentException("The provided RealmResults is no longer valid, the Realm instance it belongs to is closed. It can no longer be observed for changes.");
}
this.results = results;
if (results.isLoaded()) {
// we should not notify observers when results aren't ready yet (async query).
// however, synchronous query should be set explicitly.
setValue(results);
}
}
// We should start observing and stop observing, depending on whether we have observers.
/**
* Starts observing the RealmResults, if it is still valid.
*/
@Override
protected void onActive() {
super.onActive();
if (results.isValid()) { // invalidated results can no longer be observed.
results.addChangeListener(listener);
}
}
/**
* Stops observing the RealmResults.
*/
@Override
protected void onInactive() {
super.onInactive();
if (results.isValid()) {
results.removeChangeListener(listener);
}
}
}
|
Android LiveData: Not receiving all notifications
By : user1381329
Date : March 29 2020, 07:55 AM
it fixes the issue The explanation lies in the implementation for postValue and mPostValueRunnable: code :
protected void postValue(T value) {
boolean postTask;
synchronized (mDataLock) {
//this is true on the first run or right after the observer receives an update
postTask = mPendingData == NOT_SET;
mPendingData = value;
}
// this statement will be true if the observer hasn't received an update even though it's sent to the main looper
if (!postTask) {
return;
}
ArchTaskExecutor.getInstance().postToMainThread(mPostValueRunnable);
}
private final Runnable mPostValueRunnable = new Runnable() {
@Override
public void run() {
Object newValue;
synchronized (mDataLock) {
newValue = mPendingData;
mPendingData = NOT_SET;//once this value is reset a new mPostValueRunnable can be sent
}
// here the observer will receive the update
setValue((T) newValue);
}
};
|
Not receiving the data change at LiveData
By : ToKENiZ0R
Date : March 29 2020, 07:55 AM
should help you out You are post value to Repository MutableLiveData but observing MyViewModel LiveData. Merge them or use only one Try it like this code :
public LiveData<List<MyObj>> fetchList(){
return repository.getList();
}
mViewModel.fetchList().observe(this, new Observer<List<MyObj>>(){...}
|
Update value of LiveData everytime one of other LiveData updates its value
By : A Dey
Date : March 29 2020, 07:55 AM
it should still fix some issue You can make it more reactive-style using the extension function feature of Kotlin. Assume that you have firstLiveaData and secondLiveData with the same type of T. Now you want to filter them first and then listen to all of their changes. code :
fun <T> LiveData<T>.filter(predicate : (T) -> Boolean): LiveData<T> {
val mutableLiveData = MediatorLiveData<T>()
mutableLiveData.addSource(this) {
if(predicate(it))
mutableLiveData.value = it
}
return mutableLiveData
}
fun <T> MediatorLiveData<T>.addSources(vararg listOfLiveData: LiveData<T>, callback: (T) -> Unit) {
listOfLiveData.forEach {
addSource(it, callback)
}
}
fun <T> merge(vararg liveDataList: LiveData<T>): LiveData<T> {
val mergedLiveData = MediatorLiveData<T>()
liveDataList.forEach { liveData ->
liveData.value?.let {
mergedLiveData.value = it
}
mergedLiveData.addSource(liveData) { source ->
mergedLiveData.value = source
}
}
return mergedLiveData
}
fun doSomething() {
val firstLiveData = MutableLiveData<List<SomeType>>()
val secondLiveData = MutableLiveData<List<SomeType>>()
merge(firstLiveData, secondLiveData).filter { someFilterFunction() }.observe(...)
}
|
Android Architecture Component(LiveData): How to clear all value from LiveData once it updates a textview
By : Caulnian
Date : March 29 2020, 07:55 AM
To fix this issue You would not do this. One of the main reasons to use LiveData is to have other components be able to observe the contents of the LiveData.
|