I need to make webservice calls asynchronously in Monotouch, as the UIAlertView only shows up after work was done.
Current code (pseudo)
Class LoginviewController
{
void Login(credentials)
{
showAlert("Logging in") ;
bool authenticated = service.Authenticate(Credentials);
}
}
class Service
{
Public bool Authenticate(object Credentials)
{
object[] results = this.Invoke("GetAuth", Credentials)
}
}
I am in the process of moving the Service methods to an async model, With Authenticate being made up of BeginAuthenticate(), EndAuthenticate(), AuthenticateAsync(), OnAuthenticateOperationCompleted() and of course Authenticate().
When all of these have completed I need to run OnAuthenticateCompleted() on the LoginViewController, so I will use BeginInvokeOnMainThread(delegate....
This is where I get stuck.
How do I get the method OnAuthenticateCompleted() in LoginViewController class instance executed from the services class?
EDIT: Solution:
Added a OnAuthenticateCompleted event handler that is hooked up in Login(), and called the AuthenticateAsync() method instead of Authenticate().
Class LoginviewController
{
void Login(credentials)
{
showAlert("Logging in") ;
service.AuthenticateCompleted += new GetAuthenticationCompletedEventHandler(OnAuthenticateCompleted);
service.AuthenticateAsync(Credentials);
}
public void OnAuthenticateCompleted(obj sender, GetAuthenticationCompletedEventArgs args)
{
bool authenticated = (bool)args.Results;
//do stuff
hideAlert();
}
}
You don’t execute
LoginViewController.OnAuthenticateCompletedfrom the services class, you execute it in the completed event handler.