Sign in Go Pro

Realm & Android Architecture Components

Abstracting RealmResults from your Application to Reduce Coupling and Improve Testability

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson
Autoplay

Up next

Previous

About

Traditionally, in a Realm Application, it's been necessary to directly expose RealmResults to the various layers of your app because there was no standard way to deliver the live -- auto updating, reactive -- data that RealmResults provided. This has all changed with the advent of LiveData.

LiveData as it's name implies is a container for live, reactive data, that is also Android Lifecycle aware. This lesson will teach you how use Realm with/ LiveData to remove the dependency on RealmResults from outside of your DAO layer.

Summary of Content (what you will learn):

  • How to expose LiveData<List<? extends RealmModel>> from your DAOs.
  • Why you no longer need RealmResults when exposing LiveData.
  • The benefits that LiveData provides.

Additional Materials

Comments

Hi Eric!
When I tried to reassign the recipes LiveData with the filtered recipes second time, the onChanged method did not call somehow and hence UI did not update with the filtered list of recipes. Looks like recipes live data is unable to detect the changes. Ideally, I should be able to display any kind of recipes from the table. Any idea what could be the reason?

Hi Anamika,

Can you create a small sample that recreates this issue? I would be happy to take a look.

Thanks,
Eric

Hi Eric,

I was able to figure out the issue why it wasn't updating the live data. Basically I needed to use the MediatorLiveData in order to change the value of LiveData. Here is the solution - please let me if there is any other smartest way to achieve this.

I just took inspiration from your recipes app and created work items program. Just like in recipes example, I get all the work items in the view model constructor and display it in UI list. Look below -
mWorkItems = Transformations.map(dao.findAllAsync(), new Function, List<? extends Work>>() {
@Override
public List<? extends Work> apply(List input) {
return input;
}
});

mWorkCount = Transformations.map(mWorkItems, new Function, Integer>() {
@Override
public Integer apply(List<? extends Work> input) {
return input.size();
}
});

The requirement was that I need to apply filter on open work items and display only open work items in the list. I created different method to simply call the open work items from dao and reassign the live data object. However, it did not detect any changes. Look below -

public void getWorkOrdersByStatus(String status) {

mWorkItems = Transformations.map(dao.findByStatus(status), new Function, List<? extends Work>>() {
@Override
public List<? extends Work> apply(List input) {
return input;
}
});

mWorkCount = Transformations.map(mWorkItems, new Function, Integer>() {
@Override
public Integer apply(List<? extends Work> input) {
return input.size();
}
});
}

The above method did not work to update the live data. So I created separate MediatorLiveData variables for both mWorkItems and mWorkCount to update the changes. Look the updated method getWorkOrdersByStatus(String status):

public void getWorkOrdersByStatus(String status) {

mWorkItems = Transformations.map(dao.findByStatus(status), new Function, List<? extends Work>>() {
@Override
public List<? extends Work> apply(List input) {
return input;
}
});

mWorkCount = Transformations.map(mWorkItems, new Function, Integer>() {
@Override
public Integer apply(List<? extends Work> input) {
return input.size();
}
});

mFilteredWorkItems.addSource(mWorkItems, new Observer>()
{
@Override
public void onChanged(@Nullable List<? extends Work> works)
{
mFilteredWorkItems.setValue(works);
}
});

mFilteredWorkCount.addSource(mWorkCount, new Observer()
{
@Override
public void onChanged(@Nullable Integer integer)
{
mFilteredWorkCount.setValue(integer);
}
});
}

This somehow detected changes in the live data objects and called onChanged methods to update the list UI.

Please let me know if this solution is correct or if I can do using any other smart technique.

Thanks,
Anamika

>
You need to go PRO to post comments.

Lessons in Realm & Android Architecture Components