simolus3 / drift

Drift is an easy to use, reactive, typesafe persistence library for Dart & Flutter.
https://drift.simonbinder.eu/
MIT License
2.57k stars 366 forks source link

Stream not being updated after insert or even setState #815

Open robertocastejon opened 4 years ago

robertocastejon commented 4 years ago

Hello, I've created an App using Moor with StreamBuilder+ListView+CRUD operations and all worked well. For many reasons I've decided to duplicate the App to put new names, themes, etc. Now, on the duplicated App, the Stream fails to update the Stream+List after an Insert or Update. It works fine for Deletes. Also, as I change the watch_query parameters and call setState, on some parameters no SQL command is not fired. It's as if the query result is cached. I've looked all over the web for a solution, but so far noting worked. FYI: Issue #329 states as an issue the exact symptom that the old App has and that I'm looking for which is firing a SQL statements after each setState and reflecting Inserts and Updates. PS: Both old and new App versions have the exact same package versions and reside on the same VCS IDE. TIA

simolus3 commented 4 years ago

Thanks for the report. However, it's hard to diagnose what's going wrong here without further information.

It's as if the query result is cached.

Moor does cache the result of stream queries, but the cache is cleared when an affected table updates or when a stream has no listeners for one event loop iteration.

Stream fails to update the Stream+List after an Insert or Update

Did you change anything for the updates that fail to update stream queries?

have the exact same package versions

Which one? 3.3.1?

robertocastejon commented 4 years ago

Thanks for the prompt answer.

I understand... I was hoping that there was some kind of parameter that could be tweaked to force the SQL watch query to run on demand...

I made no change on the code except on the path parameter inside FlutterQueryExecutor.inDatabaseFolder function, that before carried the path along with a file name and now only carries the file name ("db.sqlite").

The entries on pubspec.yaml are:

version: 1.0.0+1 environment: sdk: ">=2.7.0 <3.0.0"

dependencies: flutter: sdk: flutter cupertino_icons: ^0.1.3 intl: ^0.16.0

dependency_overrides: analyzer: '0.39.14'

What I can verify, by navigating throughout the app and provoking changes to the watch query parameters, is that the SQL statement is fired every time a different SQL Select statement is formed... Once I re-execute an App option that generates a SQL statement already run before, the SQL statement is not fired again.

Similar happens after an Insert or Update, where the watch query does not run after the SQL insert|update command is executed, thus not returning the register that was just added|changed to the stream/list.

Same symptoms above are happening on the moor_db_viewer module I have inside the App, as a menu option.

Any thoughts?

Best Roberto

http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail Virus-free. www.avg.com http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

Em qui., 10 de set. de 2020 às 05:51, Simon Binder notifications@github.com escreveu:

Thanks for the report. However, it's hard to diagnose what's going wrong here without further information.

It's as if the query result is cached.

Moor does cache the result of stream queries, but the cache is cleared when an affected table updates or when a stream has no listeners for one event loop iteration.

Stream fails to update the Stream+List after an Insert or Update

Did you change anything for the updates that fail to update stream queries?

have the exact same package versions

Which one? 3.3.1?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/simolus3/moor/issues/815#issuecomment-690091316, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQF3Q3RSZYGRWO73NG3D2Y3SFCHRNANCNFSM4RECJPYA .

simolus3 commented 4 years ago

FlutterQueryExecutor.inDatabaseFolder

Are you really using a FlutterQueryExecutor? You don't depend on moor_flutter, so I assume you're now using a VmDatabase, is that correct?

Are you getting any exceptions at all? Do you get a warnings about creating database classes multiple times?

robertocastejon commented 4 years ago

Yes, I'm using FlutterQueryExecutor.inDatabeFolder to open the database (see below). Tried VmDatabase, got the same results and went back to keep it similar to the old App version. No SQL Exceptions. No multiple database class warnings with this code.

It seems that, in order to cache the query results, only the SQL statement code is being considered (similar SQL code = returns previous results cached), and not the content it returns. Can I disarm this caching mechanism by any way?

This is what I have on the Database Class:

static final TasksDatabase instance = TasksDatabase._internal();TasksDatabase._internal() : super(FlutterQueryExecutor.inDatabaseFolder(path: 'task_cast.sqlite', logStatements: true)); //, singleInstance: true));

This are the Stream references and definitions:

@override void initState() { super.initState(); stream = newStream(); }// Extracted this to reference it on setStates Stream newStream() { return TasksDatabase.instance.watchFilteredOrderedTasks(); }// Inside Scaffold: ... child: StreamBuilder<List>( stream: stream, builder: (BuildContext context, AsyncSnapshot<List> list) { ...

// Calling Insert Function await TasksDatabase.instance.addTask(Task( category: taskCategory, notificationcount: taskNotificationcount));

// Insert Functions

http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail Virus-free. www.avg.com http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

Em sex., 11 de set. de 2020 às 06:26, Simon Binder notifications@github.com escreveu:

FlutterQueryExecutor.inDatabaseFolder

Are you really using a FlutterQueryExecutor? You don't depend on moor_flutter, so I assume you're now using a VmDatabase, is that correct?

Are you getting any exceptions at all? Do you get a warnings about creating database classes multiple times?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/simolus3/moor/issues/815#issuecomment-690981024, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQF3Q3RMDY4OSX2TWDT3PRDSFHUKZANCNFSM4RECJPYA .

robertocastejon commented 4 years ago

Sorry There was a typo in the previous email Part in red does not exist. Please see below. Thanks.

Em sex., 11 de set. de 2020 às 07:03, Roberto Castejón < robertocastejon@pobox.com> escreveu:

Yes, I'm using FlutterQueryExecutor.inDatabeFolder to open the database (see below). Tried VmDatabase, got the same results and went back to keep it similar to the old App version. No SQL Exceptions. No multiple database class warnings with this code.

It seems that, in order to cache the query results, only the SQL statement code is being considered (similar SQL code = returns previous results cached), and not the content it returns. Can I disarm this caching mechanism by any way?

This is what I have on the Database Class:

static final TasksDatabase instance = TasksDatabase._internal(); *TasksDatabase._internal() : super(FlutterQueryExecutor.*inDatabaseFolder(path: 'task_cast.sqlite', logStatements: true)); //, singleInstance: true));

This are the Stream references and definitions:

@override void initState() { super.initState(); stream = newStream(); }// Extracted this to reference it on setStates Stream newStream() { return TasksDatabase.instance.watchFilteredOrderedTasks(); }// Inside Scaffold: ... child: StreamBuilder<List>( stream: stream, builder: (BuildContext context, AsyncSnapshot<List> list) { ...

// Calling Insert Function await TasksDatabase.instance.addTask(Task( category: taskCategory, notificationcount: taskNotificationcount));

// Insert Functions

  • Future addTask(Task task) async { try { return await into(tasks).insert(task); } catch (e) { await print(e); } }*

http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail Virus-free. www.avg.com http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail <#m_-7446765405225213788_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

Em sex., 11 de set. de 2020 às 06:26, Simon Binder < notifications@github.com> escreveu:

FlutterQueryExecutor.inDatabaseFolder

Are you really using a FlutterQueryExecutor? You don't depend on moor_flutter, so I assume you're now using a VmDatabase, is that correct?

Are you getting any exceptions at all? Do you get a warnings about creating database classes multiple times?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/simolus3/moor/issues/815#issuecomment-690981024, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQF3Q3RMDY4OSX2TWDT3PRDSFHUKZANCNFSM4RECJPYA .

robertocastejon commented 4 years ago

Hello, Sorry to insist, but I'm kinds stuck here. Any suggestions of what I could try? Regards, Roberto

Em sex., 11 de set. de 2020 às 07:03, Roberto Castejón < robertocastejon@pobox.com> escreveu:

Yes, I'm using FlutterQueryExecutor.inDatabeFolder to open the database (see below). Tried VmDatabase, got the same results and went back to keep it similar to the old App version. No SQL Exceptions. No multiple database class warnings with this code.

It seems that, in order to cache the query results, only the SQL statement code is being considered (similar SQL code = returns previous results cached), and not the content it returns. Can I disarm this caching mechanism by any way?

This is what I have on the Database Class:

static final TasksDatabase instance = TasksDatabase._internal();TasksDatabase._internal() : super(FlutterQueryExecutor.inDatabaseFolder(path: 'task_cast.sqlite', logStatements: true)); //, singleInstance: true));

This are the Stream references and definitions:

@override void initState() { super.initState(); stream = newStream(); }// Extracted this to reference it on setStates Stream newStream() { return TasksDatabase.instance.watchFilteredOrderedTasks(); }// Inside Scaffold: ... child: StreamBuilder<List>( stream: stream, builder: (BuildContext context, AsyncSnapshot<List> list) { ...

// Calling Insert Function await TasksDatabase.instance.addTask(Task( category: taskCategory, notificationcount: taskNotificationcount));

// Insert Functions

  • Future addTask(Task task) async { try { return await into(tasks).insert(task); } catch (e) { await print(e); } }*

http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail Virus-free. www.avg.com http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail <#m_-3242087955402046460_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

Em sex., 11 de set. de 2020 às 06:26, Simon Binder < notifications@github.com> escreveu:

FlutterQueryExecutor.inDatabaseFolder

Are you really using a FlutterQueryExecutor? You don't depend on moor_flutter, so I assume you're now using a VmDatabase, is that correct?

Are you getting any exceptions at all? Do you get a warnings about creating database classes multiple times?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/simolus3/moor/issues/815#issuecomment-690981024, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQF3Q3RMDY4OSX2TWDT3PRDSFHUKZANCNFSM4RECJPYA .

simolus3 commented 4 years ago

Sorry for the slow response. I can't reproduce this, so there's not much I can do here. If you show me a small project where moor doesn't work as you expect, I'm happy to take a look at that.

Moor has lots of tests to ensure that all kinds of updates invalidate stream queries, so without an example I'm really not sure where to look for bugs here.

robertocastejon commented 4 years ago

Ok. I’ll try stripping it down. There also the chance of it getting to work as I do so. Thanks!

Em qua, 16 de set de 2020 às 10:04, Simon Binder notifications@github.com escreveu:

Sorry for the slow response. I can't reproduce this, so there's not much I can do here. If you show me a small project where moor doesn't work as you expect, I'm happy to take a look at that.

Moor has lots of tests to ensure that all kinds of updates invalidate stream queries, so without an example I'm really not sure where to look for bugs here.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/simolus3/moor/issues/815#issuecomment-693390813, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQF3Q3VDED5D52I6XMFGAZDSGCZWXANCNFSM4RECJPYA .