How can i load data from an url on app start











up vote
2
down vote

favorite
1












In my xamarin.forms i need to load data while app start . I know that the code must be in App.cs page. I can load data on a button click, now i want to load the data while app start . what is the right solution for that










share|improve this question


























    up vote
    2
    down vote

    favorite
    1












    In my xamarin.forms i need to load data while app start . I know that the code must be in App.cs page. I can load data on a button click, now i want to load the data while app start . what is the right solution for that










    share|improve this question
























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      In my xamarin.forms i need to load data while app start . I know that the code must be in App.cs page. I can load data on a button click, now i want to load the data while app start . what is the right solution for that










      share|improve this question













      In my xamarin.forms i need to load data while app start . I know that the code must be in App.cs page. I can load data on a button click, now i want to load the data while app start . what is the right solution for that







      xamarin xamarin.forms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '15 at 7:10







      user4318551































          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          You can use async/await, if you need the download to be completed and do something with your data while you're still on the OnStart method:



          protected async override void OnStart()
          {
          // Handle when your app starts

          //retrieve your data
          TypeOfDataObject dataObj = await GetMyData();

          // Do stuff with your data
          }

          private Task<TypeOfDataObject> GetMyData()
          {
          //Get your data from URL
          return dataObj;
          }





          share|improve this answer




























            up vote
            2
            down vote













            You need to await the data and also make sure that you deal with not being able to get the data and give the user the chance to retry.



            Here is how I've done this:



            protected async override void OnStart ()
            {
            do
            {
            try
            {
            _data = await GetData();
            }
            catch(Exception e)
            {
            var errorMessage = e.Message; // Todo: Use nicer error message
            await loadingPage.DisplayAlert ("Error", errorMessage, "RETRY");
            }
            } while (_data = null);

            // Do stuff with _data
            }


            You can see a full example of this working along with displaying a loading page in my DDD North Agenda App Sample.






            share|improve this answer






























              up vote
              0
              down vote













              Just start loading data in the constructor of your App.cs. You can do it on a background thread if needed.



              Task.Run(() =>
              {
              // Do stuff in here
              });


              Use a ContinueWith to handle any exceptions.



              Update



              As per my comments below, I don't recommend this way, using the OnStart is a much better approach.



              Add an Async in there and load that way. The constructor needs to return and return with 17 seconds in the case of iOS.



              protected async override void OnStart()
              {

              }


              Also, make sure you place a try/catch around all that code because there is no Task to return an exception.






              share|improve this answer























              • thanks for replaying , is it good to adding inside 'protected override void OnStart() { // Handle when your app starts // maindata.Load_category(); }' @Adam
                – user4318551
                Nov 12 '15 at 9:22












              • yes, as @dsnunez pointed out below, you can put it in the OnStart as well, which thinking now, might be a much better place to put it rather than in the constructor as you can also use await if needed, where as it gets trickier if you are doing that in the constructor.
                – Adam Pedley
                Nov 12 '15 at 22:21








              • 1




                even though you can call an awaitable method from a constructor, you can not await it, as constructors can't have the "async" modifier
                – dsnunez
                Nov 13 '15 at 16:32






              • 1




                Well you can with a .Wait() used under strict conditions (e.g. no mixing await and Wait()) but generally its not a good idea.
                – Adam Pedley
                Nov 13 '15 at 23:01











              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',
              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%2f33666009%2fhow-can-i-load-data-from-an-url-on-app-start%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








              up vote
              3
              down vote



              accepted










              You can use async/await, if you need the download to be completed and do something with your data while you're still on the OnStart method:



              protected async override void OnStart()
              {
              // Handle when your app starts

              //retrieve your data
              TypeOfDataObject dataObj = await GetMyData();

              // Do stuff with your data
              }

              private Task<TypeOfDataObject> GetMyData()
              {
              //Get your data from URL
              return dataObj;
              }





              share|improve this answer

























                up vote
                3
                down vote



                accepted










                You can use async/await, if you need the download to be completed and do something with your data while you're still on the OnStart method:



                protected async override void OnStart()
                {
                // Handle when your app starts

                //retrieve your data
                TypeOfDataObject dataObj = await GetMyData();

                // Do stuff with your data
                }

                private Task<TypeOfDataObject> GetMyData()
                {
                //Get your data from URL
                return dataObj;
                }





                share|improve this answer























                  up vote
                  3
                  down vote



                  accepted







                  up vote
                  3
                  down vote



                  accepted






                  You can use async/await, if you need the download to be completed and do something with your data while you're still on the OnStart method:



                  protected async override void OnStart()
                  {
                  // Handle when your app starts

                  //retrieve your data
                  TypeOfDataObject dataObj = await GetMyData();

                  // Do stuff with your data
                  }

                  private Task<TypeOfDataObject> GetMyData()
                  {
                  //Get your data from URL
                  return dataObj;
                  }





                  share|improve this answer












                  You can use async/await, if you need the download to be completed and do something with your data while you're still on the OnStart method:



                  protected async override void OnStart()
                  {
                  // Handle when your app starts

                  //retrieve your data
                  TypeOfDataObject dataObj = await GetMyData();

                  // Do stuff with your data
                  }

                  private Task<TypeOfDataObject> GetMyData()
                  {
                  //Get your data from URL
                  return dataObj;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 '15 at 14:11









                  dsnunez

                  794614




                  794614
























                      up vote
                      2
                      down vote













                      You need to await the data and also make sure that you deal with not being able to get the data and give the user the chance to retry.



                      Here is how I've done this:



                      protected async override void OnStart ()
                      {
                      do
                      {
                      try
                      {
                      _data = await GetData();
                      }
                      catch(Exception e)
                      {
                      var errorMessage = e.Message; // Todo: Use nicer error message
                      await loadingPage.DisplayAlert ("Error", errorMessage, "RETRY");
                      }
                      } while (_data = null);

                      // Do stuff with _data
                      }


                      You can see a full example of this working along with displaying a loading page in my DDD North Agenda App Sample.






                      share|improve this answer



























                        up vote
                        2
                        down vote













                        You need to await the data and also make sure that you deal with not being able to get the data and give the user the chance to retry.



                        Here is how I've done this:



                        protected async override void OnStart ()
                        {
                        do
                        {
                        try
                        {
                        _data = await GetData();
                        }
                        catch(Exception e)
                        {
                        var errorMessage = e.Message; // Todo: Use nicer error message
                        await loadingPage.DisplayAlert ("Error", errorMessage, "RETRY");
                        }
                        } while (_data = null);

                        // Do stuff with _data
                        }


                        You can see a full example of this working along with displaying a loading page in my DDD North Agenda App Sample.






                        share|improve this answer

























                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          You need to await the data and also make sure that you deal with not being able to get the data and give the user the chance to retry.



                          Here is how I've done this:



                          protected async override void OnStart ()
                          {
                          do
                          {
                          try
                          {
                          _data = await GetData();
                          }
                          catch(Exception e)
                          {
                          var errorMessage = e.Message; // Todo: Use nicer error message
                          await loadingPage.DisplayAlert ("Error", errorMessage, "RETRY");
                          }
                          } while (_data = null);

                          // Do stuff with _data
                          }


                          You can see a full example of this working along with displaying a loading page in my DDD North Agenda App Sample.






                          share|improve this answer














                          You need to await the data and also make sure that you deal with not being able to get the data and give the user the chance to retry.



                          Here is how I've done this:



                          protected async override void OnStart ()
                          {
                          do
                          {
                          try
                          {
                          _data = await GetData();
                          }
                          catch(Exception e)
                          {
                          var errorMessage = e.Message; // Todo: Use nicer error message
                          await loadingPage.DisplayAlert ("Error", errorMessage, "RETRY");
                          }
                          } while (_data = null);

                          // Do stuff with _data
                          }


                          You can see a full example of this working along with displaying a loading page in my DDD North Agenda App Sample.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 19 '15 at 11:31

























                          answered Nov 13 '15 at 10:57









                          Richard Garside

                          40k86173




                          40k86173






















                              up vote
                              0
                              down vote













                              Just start loading data in the constructor of your App.cs. You can do it on a background thread if needed.



                              Task.Run(() =>
                              {
                              // Do stuff in here
                              });


                              Use a ContinueWith to handle any exceptions.



                              Update



                              As per my comments below, I don't recommend this way, using the OnStart is a much better approach.



                              Add an Async in there and load that way. The constructor needs to return and return with 17 seconds in the case of iOS.



                              protected async override void OnStart()
                              {

                              }


                              Also, make sure you place a try/catch around all that code because there is no Task to return an exception.






                              share|improve this answer























                              • thanks for replaying , is it good to adding inside 'protected override void OnStart() { // Handle when your app starts // maindata.Load_category(); }' @Adam
                                – user4318551
                                Nov 12 '15 at 9:22












                              • yes, as @dsnunez pointed out below, you can put it in the OnStart as well, which thinking now, might be a much better place to put it rather than in the constructor as you can also use await if needed, where as it gets trickier if you are doing that in the constructor.
                                – Adam Pedley
                                Nov 12 '15 at 22:21








                              • 1




                                even though you can call an awaitable method from a constructor, you can not await it, as constructors can't have the "async" modifier
                                – dsnunez
                                Nov 13 '15 at 16:32






                              • 1




                                Well you can with a .Wait() used under strict conditions (e.g. no mixing await and Wait()) but generally its not a good idea.
                                – Adam Pedley
                                Nov 13 '15 at 23:01















                              up vote
                              0
                              down vote













                              Just start loading data in the constructor of your App.cs. You can do it on a background thread if needed.



                              Task.Run(() =>
                              {
                              // Do stuff in here
                              });


                              Use a ContinueWith to handle any exceptions.



                              Update



                              As per my comments below, I don't recommend this way, using the OnStart is a much better approach.



                              Add an Async in there and load that way. The constructor needs to return and return with 17 seconds in the case of iOS.



                              protected async override void OnStart()
                              {

                              }


                              Also, make sure you place a try/catch around all that code because there is no Task to return an exception.






                              share|improve this answer























                              • thanks for replaying , is it good to adding inside 'protected override void OnStart() { // Handle when your app starts // maindata.Load_category(); }' @Adam
                                – user4318551
                                Nov 12 '15 at 9:22












                              • yes, as @dsnunez pointed out below, you can put it in the OnStart as well, which thinking now, might be a much better place to put it rather than in the constructor as you can also use await if needed, where as it gets trickier if you are doing that in the constructor.
                                – Adam Pedley
                                Nov 12 '15 at 22:21








                              • 1




                                even though you can call an awaitable method from a constructor, you can not await it, as constructors can't have the "async" modifier
                                – dsnunez
                                Nov 13 '15 at 16:32






                              • 1




                                Well you can with a .Wait() used under strict conditions (e.g. no mixing await and Wait()) but generally its not a good idea.
                                – Adam Pedley
                                Nov 13 '15 at 23:01













                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              Just start loading data in the constructor of your App.cs. You can do it on a background thread if needed.



                              Task.Run(() =>
                              {
                              // Do stuff in here
                              });


                              Use a ContinueWith to handle any exceptions.



                              Update



                              As per my comments below, I don't recommend this way, using the OnStart is a much better approach.



                              Add an Async in there and load that way. The constructor needs to return and return with 17 seconds in the case of iOS.



                              protected async override void OnStart()
                              {

                              }


                              Also, make sure you place a try/catch around all that code because there is no Task to return an exception.






                              share|improve this answer














                              Just start loading data in the constructor of your App.cs. You can do it on a background thread if needed.



                              Task.Run(() =>
                              {
                              // Do stuff in here
                              });


                              Use a ContinueWith to handle any exceptions.



                              Update



                              As per my comments below, I don't recommend this way, using the OnStart is a much better approach.



                              Add an Async in there and load that way. The constructor needs to return and return with 17 seconds in the case of iOS.



                              protected async override void OnStart()
                              {

                              }


                              Also, make sure you place a try/catch around all that code because there is no Task to return an exception.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 11 at 11:13









                              Suit Boy Apps

                              1,45432041




                              1,45432041










                              answered Nov 12 '15 at 7:58









                              Adam Pedley

                              13k54887




                              13k54887












                              • thanks for replaying , is it good to adding inside 'protected override void OnStart() { // Handle when your app starts // maindata.Load_category(); }' @Adam
                                – user4318551
                                Nov 12 '15 at 9:22












                              • yes, as @dsnunez pointed out below, you can put it in the OnStart as well, which thinking now, might be a much better place to put it rather than in the constructor as you can also use await if needed, where as it gets trickier if you are doing that in the constructor.
                                – Adam Pedley
                                Nov 12 '15 at 22:21








                              • 1




                                even though you can call an awaitable method from a constructor, you can not await it, as constructors can't have the "async" modifier
                                – dsnunez
                                Nov 13 '15 at 16:32






                              • 1




                                Well you can with a .Wait() used under strict conditions (e.g. no mixing await and Wait()) but generally its not a good idea.
                                – Adam Pedley
                                Nov 13 '15 at 23:01


















                              • thanks for replaying , is it good to adding inside 'protected override void OnStart() { // Handle when your app starts // maindata.Load_category(); }' @Adam
                                – user4318551
                                Nov 12 '15 at 9:22












                              • yes, as @dsnunez pointed out below, you can put it in the OnStart as well, which thinking now, might be a much better place to put it rather than in the constructor as you can also use await if needed, where as it gets trickier if you are doing that in the constructor.
                                – Adam Pedley
                                Nov 12 '15 at 22:21








                              • 1




                                even though you can call an awaitable method from a constructor, you can not await it, as constructors can't have the "async" modifier
                                – dsnunez
                                Nov 13 '15 at 16:32






                              • 1




                                Well you can with a .Wait() used under strict conditions (e.g. no mixing await and Wait()) but generally its not a good idea.
                                – Adam Pedley
                                Nov 13 '15 at 23:01
















                              thanks for replaying , is it good to adding inside 'protected override void OnStart() { // Handle when your app starts // maindata.Load_category(); }' @Adam
                              – user4318551
                              Nov 12 '15 at 9:22






                              thanks for replaying , is it good to adding inside 'protected override void OnStart() { // Handle when your app starts // maindata.Load_category(); }' @Adam
                              – user4318551
                              Nov 12 '15 at 9:22














                              yes, as @dsnunez pointed out below, you can put it in the OnStart as well, which thinking now, might be a much better place to put it rather than in the constructor as you can also use await if needed, where as it gets trickier if you are doing that in the constructor.
                              – Adam Pedley
                              Nov 12 '15 at 22:21






                              yes, as @dsnunez pointed out below, you can put it in the OnStart as well, which thinking now, might be a much better place to put it rather than in the constructor as you can also use await if needed, where as it gets trickier if you are doing that in the constructor.
                              – Adam Pedley
                              Nov 12 '15 at 22:21






                              1




                              1




                              even though you can call an awaitable method from a constructor, you can not await it, as constructors can't have the "async" modifier
                              – dsnunez
                              Nov 13 '15 at 16:32




                              even though you can call an awaitable method from a constructor, you can not await it, as constructors can't have the "async" modifier
                              – dsnunez
                              Nov 13 '15 at 16:32




                              1




                              1




                              Well you can with a .Wait() used under strict conditions (e.g. no mixing await and Wait()) but generally its not a good idea.
                              – Adam Pedley
                              Nov 13 '15 at 23:01




                              Well you can with a .Wait() used under strict conditions (e.g. no mixing await and Wait()) but generally its not a good idea.
                              – Adam Pedley
                              Nov 13 '15 at 23:01


















                              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%2f33666009%2fhow-can-i-load-data-from-an-url-on-app-start%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

                              List item for chat from Array inside array React Native

                              Thiostrepton

                              Caerphilly