Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancelable queries #1176

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

csm101
Copy link
Contributor

@csm101 csm101 commented Jun 21, 2023

Hi, I anticipated I was implementing this in Issue #1174 (this code contains also the small fix I proposed in pull request #1175 )

This modification extends SQLiteConnection and SQLiteAsyncConnection with a mechanism that allows to terminate long running queries by the means of setting a cancellation token.
to interrupt "sqlite server side" processing it makes use of the sqlite3_interrupt() api, that I had to map myself, since it wasn't already imported in the source code. to interrupt record fetch loops it checks the cancellation token status for each record fetched.
When an operation is aborted by the cancellation token you always get a OperationCanceledException.

I added also some tests of this feature.

for connection.Table() access methods I added a specific CancelToken(cancTok) call to be added in the "builder pattern" chain, so a cancellable query would look like this:

CancellationTokenSource tokSource= new ();

try
{
    var recs = conn.Table<MyRecType>()
        .Where(x=>....)
        .CancelToken(tokSource.Token) // <<<
        .ToList();
}
catch (OperationCanceledException)
{
   Writelog('execution has been aborted!');
}

of course tokSource.Cancel() must be executed by some other task while the query is still executing.

Whenever possible, I added an optional CancellationToken? cancTok=null parameter to existing apis, but for Apis that were expecting a params args[] for the last parameter, this wasn't possible. in such cases I defined an overloaded version that accepts the cancellation token as first parameter.

For example I had to define a sqliteConnection.Query(CancellationToken tok, string sql, params args[]) overload.


background : why I need this?
I have an android application where the end user can type a search criteria and see the results of that search criteria being found immediately every time he changes the search text, even while he is typing (actually the query is re-executed whenever the user stops typing for at least 300 ms). the problem is that, if the table contains a lot of data I really need to be able to cancel the execution of the current query (if it has not finished yet) as soon the user starts typing again.
it might be useful also in other scenarios, but in the one I need it it would really make the application much more responsive.

@csm101 csm101 mentioned this pull request Jul 14, 2023
@Dokug
Copy link

Dokug commented Aug 10, 2023

That is a great addition to the package, I hope that this gets merged soon, as I would also love to use this functionality.
Nice work!

…n was thrown instead of a OperationCanceledException
@csm101
Copy link
Contributor Author

csm101 commented Sep 22, 2023

the checks fail because of this error:
"Testhost process for source(s) '/Users/runner/work/sqlite-net/sqlite-net/tests/SQLite.Tests/bin/Debug/netcoreapp3.1/SQLite.Tests.dll' exited with error: You must install or update .NET to run this application."

I don't understand why, with this commit, is failing: I didn't change any project settings, just the .cs source file...

@csm101
Copy link
Contributor Author

csm101 commented Sep 27, 2023

But... is now sqlite-net a dead project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants