Xamarin.Android and Parse

212 Views Asked by At

I am writing an app with C# and Xamarin.I am using Parse at the backend.I am currently stuck at point where when a user clicks signup button my app checks for a user with same phone number in parse database.Ideally If app finds app makes a toast of "cannot signup".If it doesn't find a user it signs the user up. Below is the code I used.

    try {

                    var person = await(from userp in ParseUser.Query where userp.Get<string>("Phone")==phone.Text.ToString() select userp).FindAsync();
                    if(person!=null){Toast.MakeText(this,"A user with same mobile number already exist and can't signup",ToastLength.Long).Show();}
                    else{
                    signupprogress = new ProgressDialog (this);
                    signupprogress.SetTitle ("Please Wait");
                    signupprogress.SetMessage ("Signing you up!!");
                    signupprogress.SetCancelable (true);
                    signupprogress.SetProgressStyle (ProgressDialogStyle.Spinner);
                    signupprogress.Show ();
                    ParseUser user = new ParseUser ();
                    user ["Name"] = name.Text.ToString ();
                    user.Username = email.Text.ToString ();
                    user.Email = email.Text.ToString ();
                    user.Password = password.Text;
                    user ["Address"] = Address.Text.ToString ();
                    user ["Phone"] = phone.Text.ToString ();
                    await user.SignUpAsync ();
                    signupprogress.Dismiss ();
                    var inte = new Intent (this, typeof(LogIn));
                    StartActivity (inte);
                    Toast.MakeText (this, "Signed Up successfully.Please Login", ToastLength.Short).Show ();
                    }       

                } catch (Exception ep) {
                    Toast.MakeText (this, "Some erroroccured "+ ep.Message, ToastLength.Long).Show ();

                }

The problem I am facing is:

  1. I am able to signup with same number many times. If person object is not null which I think should be the case if my app finds person with same phone number the app should show the toast.
  2. If I use a different number which is not there in database then I am not able to sign up.

Please help.Thanks in Advance.

1

There are 1 best solutions below

0
On BEST ANSWER

FindAsync will never return null. It always returns a Task<IEnumerable<T>>. And the result of this task is never null either. If it doesn't find anything, it will return an empty list.
Because you are using await you don't actually have to deal with the task. You just have to check if it returned any objects.

So what you want to do is this:

var persons = await (from userp in ParseUser.Query
                     where userp.Get<string>("Phone")==phone.Text.ToString()
                     select userp).FindAsync();

if(!persons.Any())
{
    Toast.MakeText(this,
                   "A user with same mobile number already exist and can't signup",
                   ToastLength.Long)
         .Show();
}