We are using Xamarin to write C# code with SQLite for android and ios. However about how to use sqlite, I seem to have a conceptual misunderstanding:
What are the best practices for SQLite on Android?
According to the stackoverflow answer it says - one helper and one db connection. Use lock around it to make sure only one thread is accessing sqlite db at any time.
My question is - if that is the case - what is the use for async?
I tried to use async with synchronized code - and the code gave me compile errors rightfully to avoid deadlocks.
Why can't I use the 'await' operator within the body of a lock statement?
public async Task InsertAsync<T> (T item){
lock (mutex) {
await asyncConnection.InsertAsync (item);
}
}
public async Task InsertOrUpdateAsync<T> (T item){
lock (mutex) {
int count = await asyncConnection.UpdateAsync (item);
if (0 == count) {
await asyncConnection.InsertAsync (item);
}
}
}
fails. However - if I am going to use locks to ensure I am using one connection one thread at a time as a best practice in sqlite - why is there an async sqlite library at all?
And why are some threads promoting async sqlite usage on the web.
What is the real best practice for sqlite in android and iphone? Just using sync version?