How to receive data instantly with Architecture Compoenents?












0














I'm trying to implement the recommended architecture by Google and in a tutorial they show this diagram:



Architecture Components design



So I have a MainActivity and when the app starts it should go and fetch some data from the internet. I do those network operations in the Repository. Now my problem is that I don't know how to communicate properly between activities and Repository. For example MainActivity starts and immediately display a circular progress bar while Repository fetches the data. How can I stop the animation in MainActivity as soon as the data is inserted to the database? I guess I could call observe() on the LiveData and wait for onChanged(). Is there a better approach? What if there is no new data? Then onChanged() wouldn't be called...



Maybe I could send intent from Repository to MainActivity when there is no data so MainActivity knows it should stop the animation and if it doesn't receive the intent it just waits for onChanged()?



I guess I just don't feel confortable with the onChanged() method because I will never be sure of the operation it corresponds to. Maybe before the data from the network arrived there was some other data inserted which trigged onChanged() which would then stop the loading animation before it was supposed to.










share|improve this question
























  • Firstly, you don't mention ViewModel - are you using that? That is an important middleman. Secondly, are you using Room to store data, or only from the internet?
    – Knossos
    Nov 21 at 8:31










  • @Knossos Yes I'm using ViewModel and also Room to store the data in the database. The loading progress bar is a good example of my difficulties. I only want to stop that animation in MainActivity when the freshest data arrives. How do I implement that using this architecture?
    – xpland
    Nov 21 at 10:04
















0














I'm trying to implement the recommended architecture by Google and in a tutorial they show this diagram:



Architecture Components design



So I have a MainActivity and when the app starts it should go and fetch some data from the internet. I do those network operations in the Repository. Now my problem is that I don't know how to communicate properly between activities and Repository. For example MainActivity starts and immediately display a circular progress bar while Repository fetches the data. How can I stop the animation in MainActivity as soon as the data is inserted to the database? I guess I could call observe() on the LiveData and wait for onChanged(). Is there a better approach? What if there is no new data? Then onChanged() wouldn't be called...



Maybe I could send intent from Repository to MainActivity when there is no data so MainActivity knows it should stop the animation and if it doesn't receive the intent it just waits for onChanged()?



I guess I just don't feel confortable with the onChanged() method because I will never be sure of the operation it corresponds to. Maybe before the data from the network arrived there was some other data inserted which trigged onChanged() which would then stop the loading animation before it was supposed to.










share|improve this question
























  • Firstly, you don't mention ViewModel - are you using that? That is an important middleman. Secondly, are you using Room to store data, or only from the internet?
    – Knossos
    Nov 21 at 8:31










  • @Knossos Yes I'm using ViewModel and also Room to store the data in the database. The loading progress bar is a good example of my difficulties. I only want to stop that animation in MainActivity when the freshest data arrives. How do I implement that using this architecture?
    – xpland
    Nov 21 at 10:04














0












0








0







I'm trying to implement the recommended architecture by Google and in a tutorial they show this diagram:



Architecture Components design



So I have a MainActivity and when the app starts it should go and fetch some data from the internet. I do those network operations in the Repository. Now my problem is that I don't know how to communicate properly between activities and Repository. For example MainActivity starts and immediately display a circular progress bar while Repository fetches the data. How can I stop the animation in MainActivity as soon as the data is inserted to the database? I guess I could call observe() on the LiveData and wait for onChanged(). Is there a better approach? What if there is no new data? Then onChanged() wouldn't be called...



Maybe I could send intent from Repository to MainActivity when there is no data so MainActivity knows it should stop the animation and if it doesn't receive the intent it just waits for onChanged()?



I guess I just don't feel confortable with the onChanged() method because I will never be sure of the operation it corresponds to. Maybe before the data from the network arrived there was some other data inserted which trigged onChanged() which would then stop the loading animation before it was supposed to.










share|improve this question















I'm trying to implement the recommended architecture by Google and in a tutorial they show this diagram:



Architecture Components design



So I have a MainActivity and when the app starts it should go and fetch some data from the internet. I do those network operations in the Repository. Now my problem is that I don't know how to communicate properly between activities and Repository. For example MainActivity starts and immediately display a circular progress bar while Repository fetches the data. How can I stop the animation in MainActivity as soon as the data is inserted to the database? I guess I could call observe() on the LiveData and wait for onChanged(). Is there a better approach? What if there is no new data? Then onChanged() wouldn't be called...



Maybe I could send intent from Repository to MainActivity when there is no data so MainActivity knows it should stop the animation and if it doesn't receive the intent it just waits for onChanged()?



I guess I just don't feel confortable with the onChanged() method because I will never be sure of the operation it corresponds to. Maybe before the data from the network arrived there was some other data inserted which trigged onChanged() which would then stop the loading animation before it was supposed to.







android architecture android-livedata






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 8:25









Knossos

11.3k74171




11.3k74171










asked Nov 20 at 18:00









xpland

32




32












  • Firstly, you don't mention ViewModel - are you using that? That is an important middleman. Secondly, are you using Room to store data, or only from the internet?
    – Knossos
    Nov 21 at 8:31










  • @Knossos Yes I'm using ViewModel and also Room to store the data in the database. The loading progress bar is a good example of my difficulties. I only want to stop that animation in MainActivity when the freshest data arrives. How do I implement that using this architecture?
    – xpland
    Nov 21 at 10:04


















  • Firstly, you don't mention ViewModel - are you using that? That is an important middleman. Secondly, are you using Room to store data, or only from the internet?
    – Knossos
    Nov 21 at 8:31










  • @Knossos Yes I'm using ViewModel and also Room to store the data in the database. The loading progress bar is a good example of my difficulties. I only want to stop that animation in MainActivity when the freshest data arrives. How do I implement that using this architecture?
    – xpland
    Nov 21 at 10:04
















Firstly, you don't mention ViewModel - are you using that? That is an important middleman. Secondly, are you using Room to store data, or only from the internet?
– Knossos
Nov 21 at 8:31




Firstly, you don't mention ViewModel - are you using that? That is an important middleman. Secondly, are you using Room to store data, or only from the internet?
– Knossos
Nov 21 at 8:31












@Knossos Yes I'm using ViewModel and also Room to store the data in the database. The loading progress bar is a good example of my difficulties. I only want to stop that animation in MainActivity when the freshest data arrives. How do I implement that using this architecture?
– xpland
Nov 21 at 10:04




@Knossos Yes I'm using ViewModel and also Room to store the data in the database. The loading progress bar is a good example of my difficulties. I only want to stop that animation in MainActivity when the freshest data arrives. How do I implement that using this architecture?
– xpland
Nov 21 at 10:04












1 Answer
1






active

oldest

votes


















0














Regarding your issue in the comments, which I believe to answer your main question also.



You need to observe from your UI (Activity / Fragment) to a progress LiveData in your ViewModel. That could be working with a Boolean (LiveData<Boolean>). To represent the progress view being visible or not.



That in turn needs to take an identical LiveData from the Repository (declared in the Repository as a MutableLiveData). You then post updates to the progress MutableLiveData in the Repository.



Now, whenever the MutableLiveData receives a change, that exists in your ViewModel as it shares the variable reference, and it will pass to the observer in your UI.



-



Alternatively, you could return a LiveData<Boolean> from the method in your Repository that pulls data. That would then be observable in your UI.



Instead of Boolean, you could also use a more complicated structure containing more information. A message, error code, etc.






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%2f53398890%2fhow-to-receive-data-instantly-with-architecture-compoenents%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









    0














    Regarding your issue in the comments, which I believe to answer your main question also.



    You need to observe from your UI (Activity / Fragment) to a progress LiveData in your ViewModel. That could be working with a Boolean (LiveData<Boolean>). To represent the progress view being visible or not.



    That in turn needs to take an identical LiveData from the Repository (declared in the Repository as a MutableLiveData). You then post updates to the progress MutableLiveData in the Repository.



    Now, whenever the MutableLiveData receives a change, that exists in your ViewModel as it shares the variable reference, and it will pass to the observer in your UI.



    -



    Alternatively, you could return a LiveData<Boolean> from the method in your Repository that pulls data. That would then be observable in your UI.



    Instead of Boolean, you could also use a more complicated structure containing more information. A message, error code, etc.






    share|improve this answer




























      0














      Regarding your issue in the comments, which I believe to answer your main question also.



      You need to observe from your UI (Activity / Fragment) to a progress LiveData in your ViewModel. That could be working with a Boolean (LiveData<Boolean>). To represent the progress view being visible or not.



      That in turn needs to take an identical LiveData from the Repository (declared in the Repository as a MutableLiveData). You then post updates to the progress MutableLiveData in the Repository.



      Now, whenever the MutableLiveData receives a change, that exists in your ViewModel as it shares the variable reference, and it will pass to the observer in your UI.



      -



      Alternatively, you could return a LiveData<Boolean> from the method in your Repository that pulls data. That would then be observable in your UI.



      Instead of Boolean, you could also use a more complicated structure containing more information. A message, error code, etc.






      share|improve this answer


























        0












        0








        0






        Regarding your issue in the comments, which I believe to answer your main question also.



        You need to observe from your UI (Activity / Fragment) to a progress LiveData in your ViewModel. That could be working with a Boolean (LiveData<Boolean>). To represent the progress view being visible or not.



        That in turn needs to take an identical LiveData from the Repository (declared in the Repository as a MutableLiveData). You then post updates to the progress MutableLiveData in the Repository.



        Now, whenever the MutableLiveData receives a change, that exists in your ViewModel as it shares the variable reference, and it will pass to the observer in your UI.



        -



        Alternatively, you could return a LiveData<Boolean> from the method in your Repository that pulls data. That would then be observable in your UI.



        Instead of Boolean, you could also use a more complicated structure containing more information. A message, error code, etc.






        share|improve this answer














        Regarding your issue in the comments, which I believe to answer your main question also.



        You need to observe from your UI (Activity / Fragment) to a progress LiveData in your ViewModel. That could be working with a Boolean (LiveData<Boolean>). To represent the progress view being visible or not.



        That in turn needs to take an identical LiveData from the Repository (declared in the Repository as a MutableLiveData). You then post updates to the progress MutableLiveData in the Repository.



        Now, whenever the MutableLiveData receives a change, that exists in your ViewModel as it shares the variable reference, and it will pass to the observer in your UI.



        -



        Alternatively, you could return a LiveData<Boolean> from the method in your Repository that pulls data. That would then be observable in your UI.



        Instead of Boolean, you could also use a more complicated structure containing more information. A message, error code, etc.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 11:20

























        answered Nov 21 at 10:20









        Knossos

        11.3k74171




        11.3k74171






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53398890%2fhow-to-receive-data-instantly-with-architecture-compoenents%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

            Wiesbaden

            Marschland

            Dieringhausen