1

I'm using this method to update a tuple of a particular table

public async void Update(MyTable entity)
{
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection(path);
    await conn.UpdateAsync(entity); 
}

Reference: http://msdn.microsoft.com/en-us/library/windows/apps/dn263243.aspx

Now in main, i'm trying to update a record in MyTable & I'm binding MyTable records to a ViewModel which is bound to the view.

private async void button_Tapped(object sender, TappedRoutedEventArgs e)
{
    MyTableRepository.Update(scheduleRecord);

    this.DefaultViewModel["MyViewModel"] =await ViewModelClass.GetMyTableDataAsync();
}

The problem here is that the view is not getting updated. After I stop the application & check db values, the db seems to be updated & after that if i run the app again, the required updated view is displayed.

I'm new to async-await. So my feeling is that maybe ViewModel is updated even before

MyTableRepository.Update(scheduleRecord);

is executed. I actually dont know the exact cause. Please Help.

2
  • I just wanted to share that module (09) Local data discusses SQLite in detail for any developer finding this article and wanting to learn a little more. blog.jerrynixon.com/2014/10/… Commented Jan 5, 2015 at 18:32
  • Thanx @JerryNixon-MSFT. There's quite a lot of stuff to learn on the link provied. By the way I'm very sorry. The bug was in how I update MyViewModel. Actually I should have removed scheduleRecord from MyViewModel & then i should called this.DefaultViewModel["MyViewModel"] =await ViewModelClass.GetMyTableDataAsync(); So in short, my logic got wrong. Anyways a million thanx 4 ur help Commented Jan 6, 2015 at 10:44

1 Answer 1

2

Your Update method is running in a "fire and forget" fasion. When you await GetMyTableDataAsync(), the data may not yet be updated in your database.

What you need to do is change Update from async void to async Task:

public async Task UpdateAsync(MyTable entity)

and await on it:

await UpdateAsync(entity);

Your full code would look like this:

private async void button_Tapped(object sender, TappedRoutedEventArgs e)
{
    await MyTableRepository.UpdateAsync(scheduleRecord);

    this.DefaultViewModel["MyViewModel"] = await ViewModelClass.GetMyTableDataAsync();
}

As a side note:

  1. If you're not doing anything other then awaiting inside UpdateAsync, you can simply return the executing Task and await it higher up the call chain:

    public Task UpdateAsync(MyTable entity)
    {
       SQLiteAsyncConnection conn = new SQLiteAsyncConnection(path);
       return conn.UpdateAsync(entity); 
    }
    
  2. Make sure you're properly disposing your DB connections.

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

5 Comments

I changed the UpdateAsync to async task. Still the same issue... I think your 2nd point is correct, i.e. the DB connections are not disposed properly... figuring out the same at present...
Unless SQLiteAsyncConnection.UpdateAsync is synchronous masquerading as asynchronous, there's no guarantee, in your code, that the data will be saved before it's read.
@PauloMorgado Please explain. If he uses await UpdateAsync, why is there no guarantee for the update before read?
Your as in question original code, not in your answer code. The OP seemed to be in doubt that you were point out a real problem here.
I'm very sorry. The bug was in how I update MyViewModel. Actually I should have removed scheduleRecord from MyViewModel & then i should called this.DefaultViewModel["MyViewModel"] =await ViewModelClass.GetMyTableDataAsync(); So in short, my logic got wrong. Anyways a million thanx 4 ur help.

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.