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

Feature - #551 - Clearing History Via Isolate #604

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

Conversation

Vishwa-Karthik
Copy link

@Vishwa-Karthik Vishwa-Karthik commented Feb 23, 2025

Foss Hack 2025 - Clear History Via Isolate

Use of dynamic switch logic to determine the use of [Compute] function and [Isolate] function to Clear History during the app launch.

Team Dart Knight

Relevant PR for Clearing API History

Debug Considerations

  • Enrolled new enum value of 5second for HistoryRentetionPeriod for debugging purposes.
  • The final PR will have clean code

Checklist

  • [✅] I have gone through the contributing guide
  • [✅] I have updated my branch and synced it with project main branch before making this PR
  • [✅] I am using the latest Flutter stable branch (run flutter upgrade and verify)
  • [✅] I have run the tests (flutter test) and all tests are passing

Added/updated tests?

We encourage you to add relevant test cases.

  • Yes
  • No, and this is why: will complete once the approach is confirmed as correct

OS on which you have developed and tested the feature?

  • [✅] Android
  • [✅] Windows
  • macOS
  • Linux

cc: @ashitaprasad

@ashitaprasad
Copy link
Member

Let me know when this PR is ready for review.

@Vishwa-Karthik Vishwa-Karthik marked this pull request as ready for review February 23, 2025 12:15
@Vishwa-Karthik
Copy link
Author

My Findings...

Local Database Hive do not support concurrent connections or transactions due to Native OS locking the hive files on the user desired directory, (hive.lock) files.

  • Linux Based Arch Distributions and Apple System follow strict file lock system and wouldn't allow opening or accessing boxes from multiple isolates.

Reasoning

  • Hive.init and hive.initFlutter are both method-channel and main UI thread operations which needs many data accessibility which are only available in main thread isolate.

Cheap Work Around Solution

  1. Close Hive in the main thread
  2. Start an isolate
  3. Initialize Hive in that isolate
  4. Offload tasks to new isolate & Close Hive
  5. Re-Open Hive Box in the main thread

Problems

  • Although the database transactions are fast, there are high chances the database behavior becomes highly unpredictable.
  • The cleaning service job trigger logic had to changed, since calling it main function may become stupidity.

Technical Issues

  • With issues stated, frequent switches between threads will make too many open/close hive boxes to hinder performance of the app.
  • App may stop working abruptly
  • IOS Production app may not allow these operations to do so, and may kill the app.
  • The Hive documentation clearly states that it is not designed for multi-threaded use.
  • Simultaneous reads/writes across isolates may lead to inconsistencies.

What about... PR 604

  • The reason why it could have worked for Android, is due to its lenient OS behavior although its Linux-based distribution.
  • Even though Android doesn't throw an error, it's still not safe to open Hive in multiple isolates.

Note

  • There is another database called 'Isar' which probably supports multi-threaded concurrent transactions which could have been possible to resolve this functionality.

Conclusion

  • The issue opened is very real but the way it has to be tackled is just to clear them in main isolate using optimization techniques like batch request, clearing history in frequent intervals and few more everything in Main Thread ONLY.

cc: @ashitaprasad

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

Successfully merging this pull request may close these issues.

2 participants