How can i load data from an url on app start
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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
xamarin xamarin.forms
asked Nov 12 '15 at 7:10
user4318551
add a comment |
add a comment |
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;
}
add a comment |
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.
add a comment |
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.
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
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Nov 12 '15 at 14:11
dsnunez
794614
794614
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 19 '15 at 11:31
answered Nov 13 '15 at 10:57
Richard Garside
40k86173
40k86173
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
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.
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.
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%2f33666009%2fhow-can-i-load-data-from-an-url-on-app-start%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