I have a system where all database operations (SQLite) must be run through the use of a work processor so i can schedule them however i like.
Right now the processor has a Task<TResult> PostAsync<TResult>(Func<SQLiteConnection, TResult> dbFunc)
but the only sane implementation i've been able to come up with is a stack-based UI-thread worker loop that leverages Task
to be able to defer the result up to the moment the actual action is processed.
It works fine but it is clogging up the UI thread with the SQLite calls. I'd rather have that not happen but the only idea i have been able to come up with is to spawn a worker thread to flush the queue or to give each action it's own worker thread. I don't think either one of these is the most sane approach as they'd end up creating threads everytime DB access is needed.
How can i achieve an implementation where the dbFunc
arguments in the stack are executed in a background thread but said thread is only created once or is as lightweight as possible?
This has to work in a Xamarin environment. Either in PCL or Android+iOS implementations.