Calling Async task in button click in xamarin.forms - xamarin.forms

I have xamarin.forms app contains a listview which will load values from Rest API.Which is working fine.I have button just above the listview.When I click on the button, the listview API call will be placed again and the listview should update. But stuck at this update part.I am not using MVVM pattern.The listview listing portion is an async Task.I am calling the async task again when the button click, but App gets crash. Is it due to calling the async task again from button click? Any help is appreciated.
Here is My code.
namespace app
{
public partial class List : ContentPage
{
PendingWeekRange pendingWeekRange = new PendingWeekRange();
public TimeSheetList()
{
InitializeComponent();
Task.Run(async () =>
{
await LoadScreenItems();
});
}
async Task LoadScreenItems()
{
await Task.Run(async () => {
try
{
// Doing some stuff
await loadTimeSheetList();
}
catch (Exception)
{
}
});
}
async Task loadTimeSheetList()
{
await Task.Run(() => { + string postdataForPendingList = "{\"date\":\"" + "1" + "\"}";
APICall callForAPICallResult = new APICall("/API/ListMobile/ListForApproval", postdataForList, loadingIndicator);
try
{
List<ListData> resultObjForPendingTimeSheetList = callForAPICallResult<List<ListData>>();
if (resultObjForPendingTimeSheetList != null)
{
TimesheetList.ItemsSource = resultObjForPendingTimeSheetList;
screenStackLayout.VerticalOptions = LayoutOptions.FillAndExpand;
TimesheetList.IsVisible = true;
}
else
{
}
}
catch (Exception)
{
}
});
}
async void Button_Tapped(object sender, EventArgs e)
{
try
{
// Calling my listview again. After calling app gets crash
Task.Run(async () => await loadTimeSheetList());
}
catch (Exception ex) { }
}
}
}

A few things before getting to the problem. You've got async/await all wrong, go though Async Programming
Task.Run runs the passed action on a different thread, if you make changes to UI elements on this thread, your app will definitely(take my word) crash.
If you want to make async call at page launch, make use of OnAppearing method (if you only want to call once, maintain a flag)
Do not change the ItemsSource of a list view frequently, just clear and add items to it.
namespace app
{
public partial class List : ContentPage
{
PendingWeekRange pendingWeekRange = new PendingWeekRange();
private ObservableCollection<ListData> TimesheetObservableCollection = new ObservableCollection<ListData>();
public TimeSheetList()
{
InitializeComponent();
TimesheetList.ItemsSource = TimesheetObservableCollection;
}
protected override async OnAppearing()
{
// flag for first launch?
await LoadScreenItems();
}
async Task LoadScreenItems()
{
try
{
// Doing some stuff
TimesheetObservableCollection.Clear();
TimesheetObservableCollection.AddRange(await GetTimeSheetList());
}
catch (Exception)
{
//handle exception
}
}
async Task<List<ListData>> GetTimeSheetList()
{
string postdataForPendingList = "{\"date\":\"" + "1" + "\"}";
APICall callForAPICallResult = new APICall("/API/ListMobile/ListForApproval", postdataForList, loadingIndicator);
try
{
return callForAPICallResult<List<ListData>>();
}
catch (Exception)
{
// handle exception
}
}
async void Button_Tapped(object sender, EventArgs e)
{
try
{
// Calling my listview again. After calling app gets crash
TimesheetObservableCollection.Clear();
TimesheetObservableCollection.AddRange(await GetTimeSheetList());
}
catch (Exception ex) { }
}
}
}

#Androdevil,
Update your loadTimeSheetList with this,
async Task loadTimeSheetList()
{
try
{
// I am calling my API for Listview here.
List<TimeSheetListData> resultObjForPendingTimeSheetList = await callForPendingTimeSheetList.APICallResult<List<TimeSheetListData>>();
if (resultObjForPendingTimeSheetList != null)
{
TimesheetList.ItemsSource = resultObjForPendingTimeSheetList;
screenStackLayout.VerticalOptions = LayoutOptions.FillAndExpand;
TimesheetList.IsVisible = true;
}
else
{
}
}
catch (Exception)
{
}
}

Related

MainPage fails to Load properly

I have this code to set the Mainpage of my Application.
public async Task LoadMainPage()
{
try
{
signedin = Auth.IsUserSignedIn();
if (signedin)
registered = await Firestore.IsUserRegistered();
if (!signedin)
{
MainPage = new NavigationPage(new MainPage());
}
else if (signedin && !(registered))
{
MainPage = new NavigationPage(new RegistrationPage());
}
else if (signedin && (registered))
{
MainPage = new NavigationPage(new FlyoutPage1());
}
}
catch (Exception ex) { await App.Current.MainPage.DisplayAlert("Error", ex.Message, "OK"); }
}
I am calling this method from App.Xaml.cs Onstart method
protected override async void OnStart()
{
base.OnStart();
await LoadMainPage();
}
the problem is when the Application is launched the Mainpage fails to Load Properly.
it appears only when i tap the triple line icon on my phone wait for some time and tap the application again to launch it.
the code for Firestore.IsUserRegistered is as below
public async Task<bool> IsUserRegistered()
{
FirebaseFirestore collection = FirebaseFirestore.Instance;
string email = FirebaseAuth.Instance.CurrentUser.Email;
Android.Gms.Tasks.Task task = collection.Collection("User").WhereEqualTo("Email", email)
.Limit(1).Get().AddOnSuccessListener(this);
for (int i = 0; i < 25; i++)
{
await Task.Delay(100);
if (isregistered ) break;
}
return isregistered;
}
public void OnSuccess(Java.Lang.Object result)
{
var documents = (QuerySnapshot)result;
isregistered = !documents.IsEmpty;
}
You can add await LoadMainPage(); to OnResume() just like Splash Screen.
It said:
The startup work is performed asynchronously in OnResume. This is necessary so that the startup work does not slow down or delay the appearance of the launch screen. When the work has completed, SplashActivity will launch MainActivity and the user may begin interacting with the app.

Getting an exception Android.Util.AndroidRuntimeException: Only the original thread that created a view hierarchy can touch its views

Here is the Code and where i am sending request to fetching the data and first time it is loading fast and when i am coming back from anuyother page then this page is taking too much time that too when i am using Device.BeginInvokeOnMainThread() within Task.Run().
==============================================================================
public Home()
{
if (HomeModel.GetInstance().GetHomeDataPopulate())
{
FireContentPageInitialized();
}
else
{
CallRequest();
}
}
private async void CallRequest()
{
await Task.Run(async () =>
{
await Task.Delay(3000);
SetBannerData();
SetBrandsData();
SetHotDealsData();
SetNewlyAddedData();
SetRecentlyViewed();
SetRecentlyPurchased();
});
}
//Sending Requests to fetching data in subscribed events...
private void SetBannerData()
{
ModelController.OnGetHomeBannerDataSuccess += ModelController_OnGetHomeBannerDataSuccess;
ModelController.OnGetHomeBannerDataFailure += ModelController_OnGetHomeBannerDataFailure;
ModelController.FetchBannerData();
}
//Fetching Data and binding with UI Itemsource...
private void ModelController_OnGetHomeBannerDataSuccess(object sender, CustomEventArgs eventArgs)
{
stackBanner.ItemsSource = null;
stackBanner.ItemsSource = HomeModel.GetInstance().GetHomeBanners();
}
==============================================================================
This is my Code..and i added comment line where i am binding UI. I can't add lots of code here because it's too lengthy so i have added only one method which is binding UI and other methods are also doing the same.
You are trying to update view by another thread. To update view created by main thread from another thread you must do something like this. Example pseudo code (I don't know your methods).
await Task.Run(() => {
try
{
Device.BeginInvokeOnMainThread(() =>
{
SetBannerData();
SetBrandsData();
SetHotDealsData();
SetNewlyAddedData();
SetRecentlyViewed();
SetRecentlyPurchased();
}
}
catch (Exception ex)
{
}

can not show alertcontroller when page is displayed using PushModalAsync

I am trying to show toast message in android and iOS from xamarin.forms project using Dependency Service. In iOS project message is shown on MainPage or NavigationPage. but when I navigate a second page on button click using PushModalAsync, message is not displayed.
How I navigate the page
public LoginPage()
{
Device.BeginInvokeOnMainThread(() =>
{
CustomToast.LongMessage("Hiiiiii"); // Message shown
});
Navigation.PushModalAsync(new RegisterPage()); //Doesn't show
//var reg = new RegisterPage();
//Application.Current.MainPage = reg; // toast shown here
}
Code for alertController in iOS :
const double SHORT_DELAY = 2.0;
NSTimer alertDelay;
UIAlertController alert;
public void LongAlert(string message)
{
ShowAlert(message, LONG_DELAY);
}
public void ShortAlert(string message)
{
ShowAlert(message, SHORT_DELAY);
}
void ShowAlert(string message, double seconds)
{
try
{
if (alert == null && alertDelay == null)
{
alertDelay = NSTimer.CreateScheduledTimer(seconds, (obj) =>
{
Device.BeginInvokeOnMainThread(() =>
{
DismissMessage();
});
});
Device.BeginInvokeOnMainThread(() =>
{
try
{
alert = UIAlertController.Create("", message, UIAlertControllerStyle.ActionSheet);
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
}
catch (Exception ex)
{
var Error = ex.Message;
}
});
}
}
catch (Exception ex)
{
TraceLog("Message iOS ShowAlert : " + ex.Message);
}
}
void DismissMessage()
{
if (alert != null)
{
alert.DismissViewController(true, null);
alert = null;
}
if (alertDelay != null)
{
alertDelay.Dispose();
alertDelay = null;
}
}
And I call this from my register page constructor
Device.BeginInvokeOnMainThread(() =>
{
CustomToast.LongMessage("Hiiiiii");
});
It doesn't go in catch anywhere but its not displayed also. can anyone please suggest some advice ?
This is because RegisterPage is a presented page on your LoginPage, UIApplication.SharedApplication.KeyWindow.RootViewController this code can't retrieve a correct view controller for RegisterPage. It just presented an action sheet on the previous page, but your app has reached a new page then this Toast can be shown on the screen.
Firstly, you have to find out the top page on the window:
UIViewController topViewControllerWithRootViewController(UIViewController rootViewController)
{
if (rootViewController is UITabBarController)
{
UITabBarController tabBarController = (UITabBarController)rootViewController;
return topViewControllerWithRootViewController(tabBarController.SelectedViewController);
}
else if (rootViewController is UINavigationController)
{
UINavigationController navigationController = (UINavigationController)rootViewController;
return topViewControllerWithRootViewController(navigationController.VisibleViewController);
}
else if (rootViewController.PresentedViewController != null)
{
UIViewController presentedViewController = rootViewController.PresentedViewController;
return topViewControllerWithRootViewController(presentedViewController);
}
return rootViewController;
}
Secondly, adjust your presenting code like:
Device.BeginInvokeOnMainThread(() =>
{
try
{
alert = UIAlertController.Create("", messages, UIAlertControllerStyle.ActionSheet);
topViewControllerWithRootViewController(UIApplication.SharedApplication.KeyWindow.RootViewController).PresentViewController(alert, true, null);
}
catch (Exception ex)
{
var Error = ex.Message;
}
});
At last, you could show your toast using Navigation.PushModalAsync(new RegisterPage());

Async Co-Routine in Kotlin

I never use async in Kotlin. I'm not sure whether I understand is correctly.
I need that the method buttonChange(result) wait the thread is finish, to obtain the result.
fun sendConnection(view: View) {
var result = ""
if (!connected) {
async {
val runnable = Runnable()
{
result = me.connect("connection")
}
val threadSend = Thread(runnable)
threadSend.start()
}
buttonChange(result)
}
catch (e: Exception) {}
} else {
try {
async {
val runnable = Runnable()
{
result = me.connect("disconnection")
}
val threadSend = Thread(runnable)
threadSend.start()
}
buttonChange(result)
} catch (e: Exception) {
}
}
The pattern you should use is async/await.
It'll return a Deferred from async { } which you can use to call await() on. Since buttonChange seems to need the UI context, you might need to launch the coroutines as well.
launch(UI) {
try {
val result = async { me.connect("disconnection") }
buttonChange(result.await())
} catch (_: Exception) { }
}
You should not create a thread manually.

Why cant change textbox text in client function SignalR

I just start testing signalr and I am trying to add text to a rich text box after I got a response from my HUB class , but it doesn't work (no text is shown in my richtextbox) I don't know why...(the code run with no errors)
//server
public class ConnectByHub : Hub
{
public void testFunc(mas) {
string ans = mas + " got it";
Clients.All.testFunc(ans);
} }
//Client
private async void connectToServer()
{
Connection = new HubConnection(LocalClient);
HubProxy = Connection.CreateHubProxy("ConnectByHub");
try
{
await Connection.Start();
}
catch (Exception ex)
{
return;
}
string msg = "Hello friend!";
HubProxy.Invoke("testFunc", (msg)).Wait();
// Option one - doesn't work
HubProxy.On<string>("testFunc", (param) => Invoke((Action)(() => { MsgTxtBox.Text = "something happened"; })));
//Option two - doesn't work
HubProxy.On<string>("testFunc", (param) => this.Invoke((Action)(() => { MsgTxtBox.AppendText("Something happend " + Environment.NewLine); })));
}
I think part of the problem is trying to send a message from the same Async method (connectToServer) in which your listener is running.
I mostly used the same code from the question but moved a couple things around:
Moved HubProxy.Invoke() out of the Async method and called it from a button_click event
Called string.format() on the parameter
SERVER:
public class ConnectByHub : Hub
{
public void Send(string message)
{
Clients.All.testFunc(message);
}
}
CLIENT:
// Added button event
private void button1_Click(object sender, EventArgs e)
{
string msg = "Hello friend!";
HubProxy.Invoke("Send", msg).Wait();
}
private async void ConnectToServerAsync()
{
Connection = new HubConnection(LocalClient);
HubProxy = Connection.CreateHubProxy("ConnectByHub");
// Put the parmater in string.format()
HubProxy.On<string>("testFunc", (param) => this.Invoke((Action)(() => MsgTxtBox.AppendText(string.Format("{0}", param)))));
try
{
await Connection.Start();
}
catch (Exception ex)
{
richTextBox1.AppendText(string.Format("Unable to Connect to server ({0})", ServerURI));
return;
}
}

Resources