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

Got UI stuck when writting data to SQL #3138

Open
lucasjinreal opened this issue Aug 10, 2024 · 7 comments
Open

Got UI stuck when writting data to SQL #3138

lucasjinreal opened this issue Aug 10, 2024 · 7 comments

Comments

@lucasjinreal
Copy link

I have using used

LazyDatabase _openConnection() {
  // the LazyDatabase util lets us find the right location for the file async.
  return LazyDatabase(() async {
    final dbFolder = await getApplicationDocumentsDirectory();
    final file = File(p.join(dbFolder.path, 'chat2.sqlite'));
    if (!await file.exists()) {
      dbFolder.create();
    }
    // return NativeDatabase(file);
    return NativeDatabase.createInBackground(file, setup: (rawDb) {
      rawDb.execute('PRAGMA journal_mode=WAL;');
    });
  });
}

NativeDatabase.createInBackground created the database, but still, when inserting data to DB, the UI will stuck.

Is there any else reason could lead to this?

@simolus3
Copy link
Owner

Does this happen for any query or only for large batches/queries with lots of rows?

Building SQL and interpreting results still happens on the main isolate, and in some cases that can be costly. If you have more information about the kind of queries that you're running which might slow things down, I can suggest some mechanisms to move these onto other isolates as well.

@lucasjinreal
Copy link
Author

Hello, I merely have a stream that listens to all data. Every time new data arrives, the stream would update and subsequently update the UI. As of now, when I insert a single piece of data, the UI becomes stuck. I am not certain if this occurs due to my listening to the stream. However, if I do not listen to it, the UI will not update when data is inserted.

@dickermoshe
Copy link
Collaborator

Does it jank for a frame or 2, or does the app freeze.

A stream which has no listeners doesn't issue events, try adding adding a listener to the stream manually and see what happends

stream.addListener((event){
  print(event);
});

A code sample and a screen recording are needed to further diagnose this

@lucasjinreal
Copy link
Author

Hello, I believe it is caused by my listening to the stream all at once (without applying a limit), which implies that it queries all at once for each instance, approximately 1000 records every single time.

Do you think this will result in a significant amount of compute overhead? What is the most optimal approach to handle this? Is the application of a limit necessary?

@dickermoshe
Copy link
Collaborator

If you are performing multiple mutations rapidly, use a transaction so that the query only reruns once all the actions are done

@lucasjinreal
Copy link
Author

@dickermoshe I just inserted a single record, and the listened stream got update, stuck UI.
is a single record also compute densely

@dickermoshe
Copy link
Collaborator

dickermoshe commented Aug 14, 2024 via email

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

No branches or pull requests

3 participants