sqlite-net
sqlite-net copied to clipboard
Mixing Connection-Types? (async and sync connections on same database)
propably more a question…
I see some weird exceptions and "hangs" in our apps and while i'm researching best-practices for using SQLite in a Xamarin-Forms (or MAUI) App, i came along this question:
Is it save, to have TWO connections on a single Database, where one is the SQLiteConnection
and the other is the SQLiteAsyncConnection
?
Why i'm asking this:
Currently i change a lot of code to use async/await in a better/correct way in our code (avoiding .Result
and .GetAwaiter().GetResult()
when using SQLiteAsyncConnection - which hopefully solves weird "hangs" in the App (.Result
might cause DeadLocks as we all know)
But there are times when i only have a synchronous function and i need to call a Database .Table<myType>
or a .Update(item)
or something …
Now, to avoid a asyncConnection.UpdateAsync(item).Result
I'm thinking about using a syncConnection.Update(item)
instead
But that means, i need to have TWO Connections in my App - one async, the other sync - and using them depending on having an async function or not
So is it OK to use both connection-types on one Database in the same app parallel to each other?
(a next step will be to encapsulate every call inside a SemaphoreSlim or something as i read that concurrent threads reading and writing in the same database can cause problems too… but that's another story)