Android MVVM: Does using Glide directly in a fragment break the MVVM pattern?
I'm trying to follow the MVVM pattern in a new App I'm currently writing.
Basically it gets a list of items in JSON from my REST-Backend and displays it in a RecycleView inside my fragment.
I created a repository, which fetches the data and hands it over to the ViewModel which has LiveData which is observed by the fragment.
That all works fine.
But: Every item also has a url for an icon. When the list is fetched, for every item I want to load the icon from this url into a ImageView.
Actually I am using Glide to directly (asynchronously) load the icon into the corresponding ImageView - which is good for UX and performance (in my opinion), since the user already sees data while the icons load in the background
My question:
Does using Glide directly in the fragment break the MVVM pattern?
What's an alternative approach to that?
E.g. loading the icons in the Repository, updating the RecycleView every time a icon is fetched (bad performance)?
android mvvm android-glide android-architecture-components android-viewmodel
add a comment |
I'm trying to follow the MVVM pattern in a new App I'm currently writing.
Basically it gets a list of items in JSON from my REST-Backend and displays it in a RecycleView inside my fragment.
I created a repository, which fetches the data and hands it over to the ViewModel which has LiveData which is observed by the fragment.
That all works fine.
But: Every item also has a url for an icon. When the list is fetched, for every item I want to load the icon from this url into a ImageView.
Actually I am using Glide to directly (asynchronously) load the icon into the corresponding ImageView - which is good for UX and performance (in my opinion), since the user already sees data while the icons load in the background
My question:
Does using Glide directly in the fragment break the MVVM pattern?
What's an alternative approach to that?
E.g. loading the icons in the Repository, updating the RecycleView every time a icon is fetched (bad performance)?
android mvvm android-glide android-architecture-components android-viewmodel
Just suggesting what i would do, take method inViewModel
having this glide logic in it and call it from fragment by passing yourImageView
in it.
– Jeel Vankhede
Nov 22 '18 at 9:11
1
IMO, it's better to use Glide with DataBinding in your MVVM architecture.
– Ümañg ßürmån
Nov 22 '18 at 9:20
I think its fine because it a view logic.
– NIKHIL MAURYA
Nov 22 '18 at 9:34
add a comment |
I'm trying to follow the MVVM pattern in a new App I'm currently writing.
Basically it gets a list of items in JSON from my REST-Backend and displays it in a RecycleView inside my fragment.
I created a repository, which fetches the data and hands it over to the ViewModel which has LiveData which is observed by the fragment.
That all works fine.
But: Every item also has a url for an icon. When the list is fetched, for every item I want to load the icon from this url into a ImageView.
Actually I am using Glide to directly (asynchronously) load the icon into the corresponding ImageView - which is good for UX and performance (in my opinion), since the user already sees data while the icons load in the background
My question:
Does using Glide directly in the fragment break the MVVM pattern?
What's an alternative approach to that?
E.g. loading the icons in the Repository, updating the RecycleView every time a icon is fetched (bad performance)?
android mvvm android-glide android-architecture-components android-viewmodel
I'm trying to follow the MVVM pattern in a new App I'm currently writing.
Basically it gets a list of items in JSON from my REST-Backend and displays it in a RecycleView inside my fragment.
I created a repository, which fetches the data and hands it over to the ViewModel which has LiveData which is observed by the fragment.
That all works fine.
But: Every item also has a url for an icon. When the list is fetched, for every item I want to load the icon from this url into a ImageView.
Actually I am using Glide to directly (asynchronously) load the icon into the corresponding ImageView - which is good for UX and performance (in my opinion), since the user already sees data while the icons load in the background
My question:
Does using Glide directly in the fragment break the MVVM pattern?
What's an alternative approach to that?
E.g. loading the icons in the Repository, updating the RecycleView every time a icon is fetched (bad performance)?
android mvvm android-glide android-architecture-components android-viewmodel
android mvvm android-glide android-architecture-components android-viewmodel
edited Nov 22 '18 at 9:06
Ümañg ßürmån
3,1283930
3,1283930
asked Nov 22 '18 at 9:02
Bastian C. SharpBastian C. Sharp
134
134
Just suggesting what i would do, take method inViewModel
having this glide logic in it and call it from fragment by passing yourImageView
in it.
– Jeel Vankhede
Nov 22 '18 at 9:11
1
IMO, it's better to use Glide with DataBinding in your MVVM architecture.
– Ümañg ßürmån
Nov 22 '18 at 9:20
I think its fine because it a view logic.
– NIKHIL MAURYA
Nov 22 '18 at 9:34
add a comment |
Just suggesting what i would do, take method inViewModel
having this glide logic in it and call it from fragment by passing yourImageView
in it.
– Jeel Vankhede
Nov 22 '18 at 9:11
1
IMO, it's better to use Glide with DataBinding in your MVVM architecture.
– Ümañg ßürmån
Nov 22 '18 at 9:20
I think its fine because it a view logic.
– NIKHIL MAURYA
Nov 22 '18 at 9:34
Just suggesting what i would do, take method in
ViewModel
having this glide logic in it and call it from fragment by passing your ImageView
in it.– Jeel Vankhede
Nov 22 '18 at 9:11
Just suggesting what i would do, take method in
ViewModel
having this glide logic in it and call it from fragment by passing your ImageView
in it.– Jeel Vankhede
Nov 22 '18 at 9:11
1
1
IMO, it's better to use Glide with DataBinding in your MVVM architecture.
– Ümañg ßürmån
Nov 22 '18 at 9:20
IMO, it's better to use Glide with DataBinding in your MVVM architecture.
– Ümañg ßürmån
Nov 22 '18 at 9:20
I think its fine because it a view logic.
– NIKHIL MAURYA
Nov 22 '18 at 9:34
I think its fine because it a view logic.
– NIKHIL MAURYA
Nov 22 '18 at 9:34
add a comment |
3 Answers
3
active
oldest
votes
Loading an image is something called "low level detail". In other words it's something that Architecture should not care about. Hence If you use Glide or Picasso it's not relevant to Architecture of the app. Based on this your current state is "ok", however your suggestion for alternative way of doing thing can cross so many red lines.
Skim through Uncle Bob's Clean Architecture for more details.
add a comment |
In my opinion, I think it break MVVM but I think it still ok
Example: If we don't use any library for load image from url, we will create a function to get the bitmap from url (like ImageRepository#getImageBitmap(url)
), after we receive the bitmap, we will use it to display into ImageView
.
Why getImageBitmap(url)
should be inside Repository
? // because it get data from server
However, image loading library
handle all for us and it support many great things and also loading image from url just a small task (and we don't need to test it if we using library). Therefore, I think we can load image inside View
(Activity
,Fragment
,...) to make coding easier without any problem.
If we use another approach (like your approach), code will become more complex and also we need more time to test.
This is just my opinion, hope it help
"Why getImageBitmap(url) should be inside Repository? because it get data from server". But it's data access, I think it's DAO's responsibility.
– Arash
Dec 11 '18 at 10:40
add a comment |
you can use BindingAdapters to set the image from XML itself. I think it's a much more cleaner approach, so that UI related changes goes with inside the XML
@BindingAdapter("imageUrl")
fun setImageUrl(imageView: ImageView, url: String?) {
GlideApp.with(imageView)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView)
}
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%2f53427214%2fandroid-mvvm-does-using-glide-directly-in-a-fragment-break-the-mvvm-pattern%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Loading an image is something called "low level detail". In other words it's something that Architecture should not care about. Hence If you use Glide or Picasso it's not relevant to Architecture of the app. Based on this your current state is "ok", however your suggestion for alternative way of doing thing can cross so many red lines.
Skim through Uncle Bob's Clean Architecture for more details.
add a comment |
Loading an image is something called "low level detail". In other words it's something that Architecture should not care about. Hence If you use Glide or Picasso it's not relevant to Architecture of the app. Based on this your current state is "ok", however your suggestion for alternative way of doing thing can cross so many red lines.
Skim through Uncle Bob's Clean Architecture for more details.
add a comment |
Loading an image is something called "low level detail". In other words it's something that Architecture should not care about. Hence If you use Glide or Picasso it's not relevant to Architecture of the app. Based on this your current state is "ok", however your suggestion for alternative way of doing thing can cross so many red lines.
Skim through Uncle Bob's Clean Architecture for more details.
Loading an image is something called "low level detail". In other words it's something that Architecture should not care about. Hence If you use Glide or Picasso it's not relevant to Architecture of the app. Based on this your current state is "ok", however your suggestion for alternative way of doing thing can cross so many red lines.
Skim through Uncle Bob's Clean Architecture for more details.
answered Nov 22 '18 at 9:11
shervinoxshervinox
3916
3916
add a comment |
add a comment |
In my opinion, I think it break MVVM but I think it still ok
Example: If we don't use any library for load image from url, we will create a function to get the bitmap from url (like ImageRepository#getImageBitmap(url)
), after we receive the bitmap, we will use it to display into ImageView
.
Why getImageBitmap(url)
should be inside Repository
? // because it get data from server
However, image loading library
handle all for us and it support many great things and also loading image from url just a small task (and we don't need to test it if we using library). Therefore, I think we can load image inside View
(Activity
,Fragment
,...) to make coding easier without any problem.
If we use another approach (like your approach), code will become more complex and also we need more time to test.
This is just my opinion, hope it help
"Why getImageBitmap(url) should be inside Repository? because it get data from server". But it's data access, I think it's DAO's responsibility.
– Arash
Dec 11 '18 at 10:40
add a comment |
In my opinion, I think it break MVVM but I think it still ok
Example: If we don't use any library for load image from url, we will create a function to get the bitmap from url (like ImageRepository#getImageBitmap(url)
), after we receive the bitmap, we will use it to display into ImageView
.
Why getImageBitmap(url)
should be inside Repository
? // because it get data from server
However, image loading library
handle all for us and it support many great things and also loading image from url just a small task (and we don't need to test it if we using library). Therefore, I think we can load image inside View
(Activity
,Fragment
,...) to make coding easier without any problem.
If we use another approach (like your approach), code will become more complex and also we need more time to test.
This is just my opinion, hope it help
"Why getImageBitmap(url) should be inside Repository? because it get data from server". But it's data access, I think it's DAO's responsibility.
– Arash
Dec 11 '18 at 10:40
add a comment |
In my opinion, I think it break MVVM but I think it still ok
Example: If we don't use any library for load image from url, we will create a function to get the bitmap from url (like ImageRepository#getImageBitmap(url)
), after we receive the bitmap, we will use it to display into ImageView
.
Why getImageBitmap(url)
should be inside Repository
? // because it get data from server
However, image loading library
handle all for us and it support many great things and also loading image from url just a small task (and we don't need to test it if we using library). Therefore, I think we can load image inside View
(Activity
,Fragment
,...) to make coding easier without any problem.
If we use another approach (like your approach), code will become more complex and also we need more time to test.
This is just my opinion, hope it help
In my opinion, I think it break MVVM but I think it still ok
Example: If we don't use any library for load image from url, we will create a function to get the bitmap from url (like ImageRepository#getImageBitmap(url)
), after we receive the bitmap, we will use it to display into ImageView
.
Why getImageBitmap(url)
should be inside Repository
? // because it get data from server
However, image loading library
handle all for us and it support many great things and also loading image from url just a small task (and we don't need to test it if we using library). Therefore, I think we can load image inside View
(Activity
,Fragment
,...) to make coding easier without any problem.
If we use another approach (like your approach), code will become more complex and also we need more time to test.
This is just my opinion, hope it help
answered Nov 22 '18 at 9:39
Phan Van LinhPhan Van Linh
20k11109135
20k11109135
"Why getImageBitmap(url) should be inside Repository? because it get data from server". But it's data access, I think it's DAO's responsibility.
– Arash
Dec 11 '18 at 10:40
add a comment |
"Why getImageBitmap(url) should be inside Repository? because it get data from server". But it's data access, I think it's DAO's responsibility.
– Arash
Dec 11 '18 at 10:40
"Why getImageBitmap(url) should be inside Repository? because it get data from server". But it's data access, I think it's DAO's responsibility.
– Arash
Dec 11 '18 at 10:40
"Why getImageBitmap(url) should be inside Repository? because it get data from server". But it's data access, I think it's DAO's responsibility.
– Arash
Dec 11 '18 at 10:40
add a comment |
you can use BindingAdapters to set the image from XML itself. I think it's a much more cleaner approach, so that UI related changes goes with inside the XML
@BindingAdapter("imageUrl")
fun setImageUrl(imageView: ImageView, url: String?) {
GlideApp.with(imageView)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView)
}
add a comment |
you can use BindingAdapters to set the image from XML itself. I think it's a much more cleaner approach, so that UI related changes goes with inside the XML
@BindingAdapter("imageUrl")
fun setImageUrl(imageView: ImageView, url: String?) {
GlideApp.with(imageView)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView)
}
add a comment |
you can use BindingAdapters to set the image from XML itself. I think it's a much more cleaner approach, so that UI related changes goes with inside the XML
@BindingAdapter("imageUrl")
fun setImageUrl(imageView: ImageView, url: String?) {
GlideApp.with(imageView)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView)
}
you can use BindingAdapters to set the image from XML itself. I think it's a much more cleaner approach, so that UI related changes goes with inside the XML
@BindingAdapter("imageUrl")
fun setImageUrl(imageView: ImageView, url: String?) {
GlideApp.with(imageView)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView)
}
answered Nov 22 '18 at 10:01
ABrABr
19017
19017
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%2f53427214%2fandroid-mvvm-does-using-glide-directly-in-a-fragment-break-the-mvvm-pattern%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
Just suggesting what i would do, take method in
ViewModel
having this glide logic in it and call it from fragment by passing yourImageView
in it.– Jeel Vankhede
Nov 22 '18 at 9:11
1
IMO, it's better to use Glide with DataBinding in your MVVM architecture.
– Ümañg ßürmån
Nov 22 '18 at 9:20
I think its fine because it a view logic.
– NIKHIL MAURYA
Nov 22 '18 at 9:34