Sqlite-net: RunInTransactionAsync obsolete

Created on 6 Mar 2018  Â·  6Comments  Â·  Source: praeclarum/sqlite-net

Hi @praeclarum

I would like to use RunInTransactionAsync with a SQLiteAsyncConnection but appears an "obsolete" warning". It seems I can use RunInTransactionAsync only with SQLiteConnection (not "async"). Is it correct?

Thanks
Alessandro

Most helpful comment

You should not use the overload with the async connection. You could do something like that:

asyncConnection.RunInTransactionAsync(nonAsyncConnection =>
{
    foreach (var item in items)
    {
        nonAsyncConnection.InsertOrReplace(item);
    }
});

If you use non-async methods on the connection parameter, the compiler will not complain about the ambiguity.

Hope this helps!

All 6 comments

Yes. Using the async version of the connection could lead to a deadlock.

Here is the obsolete line statement

Ok, I have read it, but how can i use Transaction in a AsyncConnection? Should I not use AsyncConnection at all?

You should not use the overload with the async connection. You could do something like that:

asyncConnection.RunInTransactionAsync(nonAsyncConnection =>
{
    foreach (var item in items)
    {
        nonAsyncConnection.InsertOrReplace(item);
    }
});

If you use non-async methods on the connection parameter, the compiler will not complain about the ambiguity.

Hope this helps!

@Weldryn has the right usage.

Now that I look at it, it doesn't seem so intuitive, but that's the price we're currently paying for correctness.

Hmm. For a xamarin application, wouldn't using the SQLiteConnection for a database synchronization activity still lock the UI. I'm just trying to avoid having the UI lock, and this seems to be doing just that.

@danielPollackGitHub
Not at all. The outer connection is asynchronous. That is why the method is
named 'RunInTransactionAsync' : it runs the commands you set inside
lambda through
a synchronous connection as an asynchronous transaction. The inner
connection used inside the transaction is the one being used asynchronously.
It does not make sense to use an asynchronous connection inside an already
asynchronous transaction.

Hope this helps !

P.S. it could freeze your UI only if you would use the 'SQLiteConnection'
in the UI context and at the same time because of the transaction. However
doing CPU intensive or I/O bound work on the UI context is most of the time
a bad idea, if you ask me...

On Tue, 21 Aug 2018, 19:10 danielPollackGitHub, notifications@github.com
wrote:

Hmm. For a xamarin application, wouldn't using the SQLiteConnection for a
database synchronization activity still lock the UI. I'm just trying to
avoid having the UI lock, and this seems to be doing just that.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/praeclarum/sqlite-net/issues/699#issuecomment-414769769,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AcWbQvBps2z7okCMeHFXFeh7BUQOL4p3ks5uTE0wgaJpZM4SeoG1
.

Was this page helpful?
0 / 5 - 0 ratings