bksubhuti / tipitaka-pali-reader

A Pali Reading app made in Flutter
21 stars 5 forks source link

laggy UI during building unique wordlist of english pali extension #179

Closed bksubhuti closed 1 year ago

bksubhuti commented 1 year ago

Circle wait and display does not show properly.. looks like "hung" but it is still working. Need to fix.. strange enough the notifier is only updating after each book is finished finding all the words.. (every 10seconds?) so rebound would probably not fix.

bksubhuti commented 1 year ago

gpt says..

The makeEnglishWordList function you've provided is an asynchronous operation that involves a substantial amount of data processing, such as database queries, data parsing, and a lot more. This heavy processing might be blocking the UI thread of your Flutter application from updating, hence causing the CircularProgressIndicator (I assume this is what you mean by 'circlewait') to appear frozen or barely turning.

Flutter uses a single-threaded model for UI updates, and any long-running tasks or heavy processing that takes place in this thread can block the UI from updating smoothly. To prevent the UI from freezing during heavy processing, you can use isolates, which allow you to run Dart code on another thread.

Here is how you can modify the makeEnglishWordList function to use an isolate:

```dart
import 'dart:isolate';

// This function will be run on an isolate
void makeEnglishWordListIsolate(SendPort sendPort) async {
  // The function's code goes here
  // Replace `downloadNotifier.message` with `sendPort.send(message)`

  // Example:
  sendPort.send("Creating unique wordlist\n");
  // Do some heavy processing...
  sendPort.send("English word list is complete");
}

Future<void> makeEnglishWordList() async {
  // Create a receive port to receive messages from the isolate
  final receivePort = ReceivePort();

  // Listen for messages
  receivePort.listen((message) {
    // Update the notifier with the received message
    downloadNotifier.message = message;
  });

  // Start the isolate
  await Isolate.spawn(makeEnglishWordListIsolate, receivePort.sendPort);
}

``Please note that when using isolates, you can't directly use or modify any variables or objects (likedownloadNotifier) from the main thread in the isolate's thread. Instead, you need to pass messages between the two threads usingSendPortandReceivePort`.

Regarding database access, Flutter's sqflite library does not support running on an isolate because it's using native plugins to access the SQLite database, and native plugins can only be used on the main thread. Therefore, you need to keep all database operations in your main thread.

The primary goal here is to ensure that the heavy processing (like parsing data, etc.) is done in the isolate, but any tasks that require UI updates or access to main-thread resources are done in the main thread.

bksubhuti commented 1 year ago

fixed by sumbodhi