Skip to content

Commit 8fc5b34

Browse files
authored
Create dark_knight_history.md
1 parent f0f26a5 commit 8fc5b34

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# History Service Implementation for Auto-Clearing API Requests
2+
3+
Initial proposals:
4+
- https://github.com/foss42/apidash/pull/577
5+
- https://github.com/foss42/apidash/pull/578
6+
7+
Issue - https://github.com/foss42/apidash/issues/551
8+
PR - https://github.com/foss42/apidash/pull/604 https://github.com/foss42/apidash/pull/616
9+
Project Link - https://fossunited.org/hack/fosshack25/p/i0aod39ham
10+
11+
## Team Dart Knight
12+
13+
+ User - `Vishwa Karthik`
14+
15+
16+
## Do Checkout
17+
+ [Issues with Isolate & Compute Methodology](https://github.com/foss42/apidash/pull/604#issuecomment-2676863276)
18+
19+
20+
## Overview
21+
22+
This document outlines the implementation of the autoClearHistory feature in APIDASH to efficiently manage stored API request history while maintaining a smooth user experience across all supported platforms.
23+
24+
## Features
25+
### Auto-Clear History on App Launch
26+
27+
+ When the app is launched, it automatically clears 50 old API records to prevent excessive local storage usage.
28+
29+
+ This helps in keeping the app responsive and prevents unnecessary memory consumption.
30+
31+
+ Triggered using autoClearHistory() from HistoryServiceImpl.
32+
33+
### Dynamic Auto-Clear Trigger (State Management-Based)
34+
35+
+ Uses Riverpod StateNotifier to monitor the API request list dynamically.
36+
37+
+ If the length exceeds 50, the history clearance is automatically triggered, just to avoid heavy duty on app launch.
38+
39+
+ Reason to use State management here is to resist unnecessary local database call because user may keep switching apps/window during development cycle.
40+
41+
### Platform-Specific Cleanup Handling
42+
43+
+ For Android & iOS: Uses AppLifecycleState.paused via WidgetsBindingObserver to trigger autoClearHistory() when the app goes to the background.
44+
45+
+ For Windows, macOS, Linux:Uses window_manager to detect app minimize events and trigger cleanup accordingly.
46+
47+
+ Ensures proper handling since AppLifecycleState does not work on desktops.
48+
49+
### Batch Deletion Strategy
50+
51+
+ Deleting 50 records at a time minimizes performance issues.
52+
53+
+ Ensures smooth UX by avoiding excessive database transactions.
54+
55+
+ Users can continue adding new requests seamlessly without experiencing lag.
56+
57+
## [Issues with Clearing via Isolates](https://github.com/foss42/apidash/pull/604)
58+
59+
## My Findings...
60+
61+
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.
62+
63+
+ Linux Based Arch Distributions and Apple System follow strict file lock system and wouldn't allow opening or accessing boxes from multiple isolates.
64+
65+
## Reasoning
66+
+ `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.
67+
68+
## Cheap Work Around Solution
69+
1. Close Hive in the main thread
70+
2. Start an isolate
71+
3. Initialize Hive in that isolate
72+
4. Offload tasks to new isolate & Close Hive
73+
5. Re-Open Hive Box in the main thread
74+
75+
## Problems
76+
+ Although the database transactions are fast, there are high chances the database behavior becomes highly unpredictable.
77+
+ The cleaning service job trigger logic had to changed, since calling it main function may become stupidity.
78+
79+
## Technical Issues
80+
+ With issues stated, frequent switches between threads will make too many open/close hive boxes to hinder performance of the app.
81+
+ App may stop working abruptly
82+
+ IOS Production app may not allow these operations to do so, and may kill the app.
83+
+ The Hive documentation clearly states that it is not designed for multi-threaded use.
84+
+ Simultaneous reads/writes across isolates may lead to inconsistencies.
85+
86+
## What about... [PR 604](https://github.com/foss42/apidash/pull/604)
87+
+ The reason why it could have worked for Android, is due to its lenient OS behavior although its Linux-based distribution.
88+
+ Even though Android doesn't throw an error, it's still not safe to open Hive in multiple isolates.
89+
90+
## Note
91+
+ There is another database called 'Isar' which probably supports multi-threaded concurrent transactions which could have been possible to resolve this functionality.
92+
93+
## Conclusion
94+
+ 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.

0 commit comments

Comments
 (0)