Basit ZIa
Basit ZIa

Reputation: 986

Get object synchronously from Parse in Xamarin

I am trying to fetch and object from class synchronously by following code:

var userName = ParseObject.GetQuery ("Users")
              .WhereEqualTo("UserName","usernameOfUser") ;
var responseUserName = userName.FindAsync();
do {
} while(!responseUserName.IsCompleted || !responseUserName.IsFaulted);
if(responseUserName.IsFaulted){
     Console.WriteLine("Faulted");
}
IEnumerable<ParseObject> result = responseUserName.Result;

but 'responseUserName' status is always "waitingForActivation".

if i am doing it wrong please tell the correct way for getting object from class synchronously.

Update: i can not use await. because for await i will have to declare method as async. but i do not want to return control to front end developer(UI developer) until i have result of the query.

Update 2: I can get result by using ContinueWith method. but it does not block the execution until i get results.

var userName = ParseObject.GetQuery (Constants.TABLE_USER)
        .WhereEqualTo(Constants.COL_USER_NAME,user.GetUserName());
Users RegisteredUser = new Users ();
var responseUserName = userName.FindAsync().ContinueWith(t=>{
        IEnumerable<ParseObject> result = t.Result;
        if (result.Count() >= 0) {
            Console.WriteLine("Got Records" + result.Count());
        }
});

Thanks

Upvotes: 1

Views: 985

Answers (1)

Servy
Servy

Reputation: 203829

You're running into the classic asynchronous deadlock. The asynchronous code that you're calling is itself calling await somewhere (or the equivalent) and it is scheduling continuations that will run in the captured synchronization context. You're blocking that context with your busywait (or even non-busy wait, when you've tried that). The asynchronous operation can't complete until you let it run, and you won't let it run until it completes. Deadlock.

By far the best option is to make this method actually be asynchronous. If you use any asynchrony at all you should "async all the way up" and make everything asynchronous. If you aren't willing to do that, then just use synchronous, rather than asynchronous, methods all the way down.

Upvotes: 5

Related Questions