Google Android Sample MVVM - Why do they use MediatorLiveData instead of passing LiveData with getter?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I was analyzing Android architecture components sample app (BasicSample). Why do they use MediatorLiveData in DataRepository class if there is only one source attached to it?
https://github.com/googlesamples/android-architecture-components/blob/master/BasicSample/app/src/main/java/com/example/android/persistence/DataRepository.java#L25-L31
mObservableProducts = new MediatorLiveData<>();
mObservableProducts.addSource(mDatabase.productDao().loadAllProducts(),
productEntities -> {
if (mDatabase.getDatabaseCreated().getValue() != null) {
mObservableProducts.postValue(productEntities);
}
});
I think they should just use LiveData<List<ProductEntity> coming from DAO and facilitate it with getter method:
public LiveData<List<ProductEntity>> getProducts() {
return mDatabase.productDao().loadAllProducts();
}
Exactly how they did with other DAO requests:
https://github.com/googlesamples/android-architecture-components/blob/master/BasicSample/app/src/main/java/com/example/android/persistence/DataRepository.java#L47-L61
public LiveData<List<ProductEntity>> getProducts() {
return mObservableProducts;
}
public LiveData<ProductEntity> loadProduct(final int productId) {
return mDatabase.productDao().loadProduct(productId);
}
public LiveData<List<CommentEntity>> loadComments(final int productId) {
return mDatabase.commentDao().loadComments(productId);
}
public LiveData<List<ProductEntity>> searchProducts(String query) {
return mDatabase.productDao().searchAllProducts(query);
}
The same question relates to ProductListViewModel. They again created MediatorLiveData only with one source.
https://github.com/googlesamples/android-architecture-components/blob/master/BasicSample/app/src/main/java/com/example/android/persistence/viewmodel/ProductListViewModel.java#L42-L46
mRepository = ((BasicApp) application).getRepository();
LiveData<List<ProductEntity>> products = mRepository.getProducts();
// observe the changes of the products from the database and forward them
mObservableProducts.addSource(products, mObservableProducts::setValue);
add a comment |
I was analyzing Android architecture components sample app (BasicSample). Why do they use MediatorLiveData in DataRepository class if there is only one source attached to it?
https://github.com/googlesamples/android-architecture-components/blob/master/BasicSample/app/src/main/java/com/example/android/persistence/DataRepository.java#L25-L31
mObservableProducts = new MediatorLiveData<>();
mObservableProducts.addSource(mDatabase.productDao().loadAllProducts(),
productEntities -> {
if (mDatabase.getDatabaseCreated().getValue() != null) {
mObservableProducts.postValue(productEntities);
}
});
I think they should just use LiveData<List<ProductEntity> coming from DAO and facilitate it with getter method:
public LiveData<List<ProductEntity>> getProducts() {
return mDatabase.productDao().loadAllProducts();
}
Exactly how they did with other DAO requests:
https://github.com/googlesamples/android-architecture-components/blob/master/BasicSample/app/src/main/java/com/example/android/persistence/DataRepository.java#L47-L61
public LiveData<List<ProductEntity>> getProducts() {
return mObservableProducts;
}
public LiveData<ProductEntity> loadProduct(final int productId) {
return mDatabase.productDao().loadProduct(productId);
}
public LiveData<List<CommentEntity>> loadComments(final int productId) {
return mDatabase.commentDao().loadComments(productId);
}
public LiveData<List<ProductEntity>> searchProducts(String query) {
return mDatabase.productDao().searchAllProducts(query);
}
The same question relates to ProductListViewModel. They again created MediatorLiveData only with one source.
https://github.com/googlesamples/android-architecture-components/blob/master/BasicSample/app/src/main/java/com/example/android/persistence/viewmodel/ProductListViewModel.java#L42-L46
mRepository = ((BasicApp) application).getRepository();
LiveData<List<ProductEntity>> products = mRepository.getProducts();
// observe the changes of the products from the database and forward them
mObservableProducts.addSource(products, mObservableProducts::setValue);
1
You are missing that they are intentionally filtering the DAO emissions before the initial data is inserted into the DB. This is what the MediatorLiveData you mentioned does.
– EpicPandaForce
Jan 4 at 22:44
Aaa OK :) So if I don't insert any data to the database at the first run I can be sure that the database was created before the first query so I can use standardpublic LiveData<List<ProductEntity>> getProducts() { return mDatabase.productDao().loadAllProducts(); }without creatingMediator?
– AppiDevo
Jan 4 at 23:10
1
Yes then you can use the standard dao without a mediator ;) technically if you didn't do the mediator dance, you could just have an empty view ready first and then the data would load when the data is inserted thanks to LiveData Room integration. So that mediator wouldn't even be strictly necessary.
– EpicPandaForce
Jan 4 at 23:17
add a comment |
I was analyzing Android architecture components sample app (BasicSample). Why do they use MediatorLiveData in DataRepository class if there is only one source attached to it?
https://github.com/googlesamples/android-architecture-components/blob/master/BasicSample/app/src/main/java/com/example/android/persistence/DataRepository.java#L25-L31
mObservableProducts = new MediatorLiveData<>();
mObservableProducts.addSource(mDatabase.productDao().loadAllProducts(),
productEntities -> {
if (mDatabase.getDatabaseCreated().getValue() != null) {
mObservableProducts.postValue(productEntities);
}
});
I think they should just use LiveData<List<ProductEntity> coming from DAO and facilitate it with getter method:
public LiveData<List<ProductEntity>> getProducts() {
return mDatabase.productDao().loadAllProducts();
}
Exactly how they did with other DAO requests:
https://github.com/googlesamples/android-architecture-components/blob/master/BasicSample/app/src/main/java/com/example/android/persistence/DataRepository.java#L47-L61
public LiveData<List<ProductEntity>> getProducts() {
return mObservableProducts;
}
public LiveData<ProductEntity> loadProduct(final int productId) {
return mDatabase.productDao().loadProduct(productId);
}
public LiveData<List<CommentEntity>> loadComments(final int productId) {
return mDatabase.commentDao().loadComments(productId);
}
public LiveData<List<ProductEntity>> searchProducts(String query) {
return mDatabase.productDao().searchAllProducts(query);
}
The same question relates to ProductListViewModel. They again created MediatorLiveData only with one source.
https://github.com/googlesamples/android-architecture-components/blob/master/BasicSample/app/src/main/java/com/example/android/persistence/viewmodel/ProductListViewModel.java#L42-L46
mRepository = ((BasicApp) application).getRepository();
LiveData<List<ProductEntity>> products = mRepository.getProducts();
// observe the changes of the products from the database and forward them
mObservableProducts.addSource(products, mObservableProducts::setValue);
I was analyzing Android architecture components sample app (BasicSample). Why do they use MediatorLiveData in DataRepository class if there is only one source attached to it?
https://github.com/googlesamples/android-architecture-components/blob/master/BasicSample/app/src/main/java/com/example/android/persistence/DataRepository.java#L25-L31
mObservableProducts = new MediatorLiveData<>();
mObservableProducts.addSource(mDatabase.productDao().loadAllProducts(),
productEntities -> {
if (mDatabase.getDatabaseCreated().getValue() != null) {
mObservableProducts.postValue(productEntities);
}
});
I think they should just use LiveData<List<ProductEntity> coming from DAO and facilitate it with getter method:
public LiveData<List<ProductEntity>> getProducts() {
return mDatabase.productDao().loadAllProducts();
}
Exactly how they did with other DAO requests:
https://github.com/googlesamples/android-architecture-components/blob/master/BasicSample/app/src/main/java/com/example/android/persistence/DataRepository.java#L47-L61
public LiveData<List<ProductEntity>> getProducts() {
return mObservableProducts;
}
public LiveData<ProductEntity> loadProduct(final int productId) {
return mDatabase.productDao().loadProduct(productId);
}
public LiveData<List<CommentEntity>> loadComments(final int productId) {
return mDatabase.commentDao().loadComments(productId);
}
public LiveData<List<ProductEntity>> searchProducts(String query) {
return mDatabase.productDao().searchAllProducts(query);
}
The same question relates to ProductListViewModel. They again created MediatorLiveData only with one source.
https://github.com/googlesamples/android-architecture-components/blob/master/BasicSample/app/src/main/java/com/example/android/persistence/viewmodel/ProductListViewModel.java#L42-L46
mRepository = ((BasicApp) application).getRepository();
LiveData<List<ProductEntity>> products = mRepository.getProducts();
// observe the changes of the products from the database and forward them
mObservableProducts.addSource(products, mObservableProducts::setValue);
edited Jan 4 at 19:07
AppiDevo
asked Jan 4 at 18:19
AppiDevoAppiDevo
1,51821832
1,51821832
1
You are missing that they are intentionally filtering the DAO emissions before the initial data is inserted into the DB. This is what the MediatorLiveData you mentioned does.
– EpicPandaForce
Jan 4 at 22:44
Aaa OK :) So if I don't insert any data to the database at the first run I can be sure that the database was created before the first query so I can use standardpublic LiveData<List<ProductEntity>> getProducts() { return mDatabase.productDao().loadAllProducts(); }without creatingMediator?
– AppiDevo
Jan 4 at 23:10
1
Yes then you can use the standard dao without a mediator ;) technically if you didn't do the mediator dance, you could just have an empty view ready first and then the data would load when the data is inserted thanks to LiveData Room integration. So that mediator wouldn't even be strictly necessary.
– EpicPandaForce
Jan 4 at 23:17
add a comment |
1
You are missing that they are intentionally filtering the DAO emissions before the initial data is inserted into the DB. This is what the MediatorLiveData you mentioned does.
– EpicPandaForce
Jan 4 at 22:44
Aaa OK :) So if I don't insert any data to the database at the first run I can be sure that the database was created before the first query so I can use standardpublic LiveData<List<ProductEntity>> getProducts() { return mDatabase.productDao().loadAllProducts(); }without creatingMediator?
– AppiDevo
Jan 4 at 23:10
1
Yes then you can use the standard dao without a mediator ;) technically if you didn't do the mediator dance, you could just have an empty view ready first and then the data would load when the data is inserted thanks to LiveData Room integration. So that mediator wouldn't even be strictly necessary.
– EpicPandaForce
Jan 4 at 23:17
1
1
You are missing that they are intentionally filtering the DAO emissions before the initial data is inserted into the DB. This is what the MediatorLiveData you mentioned does.
– EpicPandaForce
Jan 4 at 22:44
You are missing that they are intentionally filtering the DAO emissions before the initial data is inserted into the DB. This is what the MediatorLiveData you mentioned does.
– EpicPandaForce
Jan 4 at 22:44
Aaa OK :) So if I don't insert any data to the database at the first run I can be sure that the database was created before the first query so I can use standard
public LiveData<List<ProductEntity>> getProducts() { return mDatabase.productDao().loadAllProducts(); } without creating Mediator?– AppiDevo
Jan 4 at 23:10
Aaa OK :) So if I don't insert any data to the database at the first run I can be sure that the database was created before the first query so I can use standard
public LiveData<List<ProductEntity>> getProducts() { return mDatabase.productDao().loadAllProducts(); } without creating Mediator?– AppiDevo
Jan 4 at 23:10
1
1
Yes then you can use the standard dao without a mediator ;) technically if you didn't do the mediator dance, you could just have an empty view ready first and then the data would load when the data is inserted thanks to LiveData Room integration. So that mediator wouldn't even be strictly necessary.
– EpicPandaForce
Jan 4 at 23:17
Yes then you can use the standard dao without a mediator ;) technically if you didn't do the mediator dance, you could just have an empty view ready first and then the data would load when the data is inserted thanks to LiveData Room integration. So that mediator wouldn't even be strictly necessary.
– EpicPandaForce
Jan 4 at 23:17
add a comment |
1 Answer
1
active
oldest
votes
They are intentionally filtering the DAO emissions before the initial data is inserted into the DB. This is what the MediatorLiveData you mentioned does.
If you don't need to wait for an "initial set of data to be loaded" like they do, then you can just ditch the MediatorLiveData and use mDatabase.productDao().loadAllProducts(); directly.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54044162%2fgoogle-android-sample-mvvm-why-do-they-use-mediatorlivedata-instead-of-passing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
They are intentionally filtering the DAO emissions before the initial data is inserted into the DB. This is what the MediatorLiveData you mentioned does.
If you don't need to wait for an "initial set of data to be loaded" like they do, then you can just ditch the MediatorLiveData and use mDatabase.productDao().loadAllProducts(); directly.
add a comment |
They are intentionally filtering the DAO emissions before the initial data is inserted into the DB. This is what the MediatorLiveData you mentioned does.
If you don't need to wait for an "initial set of data to be loaded" like they do, then you can just ditch the MediatorLiveData and use mDatabase.productDao().loadAllProducts(); directly.
add a comment |
They are intentionally filtering the DAO emissions before the initial data is inserted into the DB. This is what the MediatorLiveData you mentioned does.
If you don't need to wait for an "initial set of data to be loaded" like they do, then you can just ditch the MediatorLiveData and use mDatabase.productDao().loadAllProducts(); directly.
They are intentionally filtering the DAO emissions before the initial data is inserted into the DB. This is what the MediatorLiveData you mentioned does.
If you don't need to wait for an "initial set of data to be loaded" like they do, then you can just ditch the MediatorLiveData and use mDatabase.productDao().loadAllProducts(); directly.
answered Jan 17 at 14:01
EpicPandaForceEpicPandaForce
51.3k15137271
51.3k15137271
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54044162%2fgoogle-android-sample-mvvm-why-do-they-use-mediatorlivedata-instead-of-passing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You are missing that they are intentionally filtering the DAO emissions before the initial data is inserted into the DB. This is what the MediatorLiveData you mentioned does.
– EpicPandaForce
Jan 4 at 22:44
Aaa OK :) So if I don't insert any data to the database at the first run I can be sure that the database was created before the first query so I can use standard
public LiveData<List<ProductEntity>> getProducts() { return mDatabase.productDao().loadAllProducts(); }without creatingMediator?– AppiDevo
Jan 4 at 23:10
1
Yes then you can use the standard dao without a mediator ;) technically if you didn't do the mediator dance, you could just have an empty view ready first and then the data would load when the data is inserted thanks to LiveData Room integration. So that mediator wouldn't even be strictly necessary.
– EpicPandaForce
Jan 4 at 23:17