Android MVVM: Does using Glide directly in a fragment break the MVVM pattern?












1















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)?










share|improve this question

























  • 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





    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
















1















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)?










share|improve this question

























  • 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





    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














1












1








1


1






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)?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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





    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








  • 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












3 Answers
3






active

oldest

votes


















0














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.






share|improve this answer































    0














    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






    share|improve this answer
























    • "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



















    0














    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)


    }






    share|improve this answer























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      0














      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.






      share|improve this answer




























        0














        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.






        share|improve this answer


























          0












          0








          0







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 9:11









          shervinoxshervinox

          3916




          3916

























              0














              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






              share|improve this answer
























              • "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
















              0














              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






              share|improve this answer
























              • "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














              0












              0








              0







              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






              share|improve this answer













              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







              share|improve this answer












              share|improve this answer



              share|improve this answer










              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



















              • "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











              0














              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)


              }






              share|improve this answer




























                0














                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)


                }






                share|improve this answer


























                  0












                  0








                  0







                  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)


                  }






                  share|improve this answer













                  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)


                  }







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 10:01









                  ABrABr

                  19017




                  19017






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      To store a contact into the json file from server.js file using a class in NodeJS

                      Redirect URL with Chrome Remote Debugging Android Devices

                      Dieringhausen