0

Last statement in InsertEmployee is FetchEmployeesAsync. But FetchEmployeesAsync has an await. According to me, control is transferred back to the caller if the awaited task is not completed. But the execution will still wait at the end of InsertEmployee and not go to its caller. Am I right?

public void InsertEmployee(Employee e)
{
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection("Employee.sqlite");
    conn.InsertAsync(e);
    FetchEmployeesAsync();
 }


public async void FetchEmployeesAsync()
{
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection("Employee.sqlite");
    employees = await conn.Table<Employee>().ToListAsync();
    DisplayList();
}

1 Answer 1

1

Because FetchEmployeesAsync awaits the conn.Table<Employee> call, it will return control to its caller while the task finishes. Since the caller in this scenario is InsertEmployee, which invokes FetchEmployees without awaiting, the InsertEmployee method will immediately finish up and exit.

When a task is awaited further down in the stack, control will be returned all the way up to the aggregate root, unless it is awaited at some point on its way to the aggregate root, which is typically (but not always) the UI.

If you want to prevent the InsertEmployee method from completing before FetchEmployees is done, you can make FetchEmployees return a Task.

public async void InsertEmployee(Employee e)
{
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection("Employee.sqlite");
    conn.InsertAsync(e);
    await FetchEmployees();
}

public async Task FetchEmployeesAsync()
{
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection("Employee.sqlite");
    employees = await conn.Table<Employee>().ToListAsync();
    DisplayList();
}

While the return value of FetchEmployeesAsync is a Task, you don't have to explicitly return one. During compilation some magic stuff happens and when the await happens, a task is returned for you with the rest of the method being marshaled in to a continuation

Sign up to request clarification or add additional context in comments.

4 Comments

Thats how i had written it previously, had an await for conn.InsertAsync(e); too.But the thing is, await would make it go to the root code, which is the same thing that happens if InsertEmployee executes when call to FetchEmployeesAsync is not awaited. Will not letting it go to the root code freeze the UI?
If you are using await on the UI thread, then it will block it. But you can use another tread for the update. But as soon as it is finished and you want the UI to be updated (binding), this has to happen again on the UI thread.
Awaiting on the UI thread will not block the UI thread. That's the whole point of the async/await pattern in windows 8 mobile dev. It prevents the method from continuing until finished, but the UI can continue to animate, refresh and be interacted with.
Ok, but it wont freeze UI only if I let it go to the root code, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.