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.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Gk_999
  • 508
  • 8
  • 29
  • 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. http://blog.jerrynixon.com/2014/10/ready-to-learn-developing-universal.html – Jerry Nixon Jan 05 '15 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 – Gk_999 Jan 06 '15 at 10:44

1 Answers1

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.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • 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... – Gk_999 Jan 05 '15 at 09:41
  • 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. – Paulo Morgado Jan 05 '15 at 10:57
  • 1
    @PauloMorgado Please explain. If he uses `await UpdateAsync`, why is there no guarantee for the update before read? – Yuval Itzchakov Jan 05 '15 at 11:37
  • 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. – Paulo Morgado Jan 05 '15 at 23:03
  • 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. – Gk_999 Jan 06 '15 at 10:45