-
Notifications
You must be signed in to change notification settings - Fork 136
Could not begin read transaction (another read transaction is still active on this thread) #288
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
Comments
That's pretty weird... Can you provide some code actually running the parallel operations? It would be perfect if you could reduce the issue to the minimal flutter app where the issue can be reproduced since I'm not familiar with I've had a brief look at |
Okay so what is happening is as the application starts, I start the foreground service. Now as I'm using a pedometer, it emits a stream of steps which we have to listen to. I'm listening to the stream in the main isolate and also in the foreground service. So when a new value occurs in the stream, my foreground service first fetches the previous steps; does some calculation, and then store that calculated steps in the DB and as the main isolate is also listening to the stream, when an event occurs there, I try to fetch the new steps from the DB so that I can show it in the UI. Now as these two the two get events are happening at the same time in different threads, it is throwing me this exception. I would really really appreciate your help here. |
That's the thing - if they were happening in different threads, then there would be no issue - the error is that it tries to start another TX on the same thread... " OK, I have a clearer picture of what you're doing and with a little more info I may be able to try and reproduce it... Could you please add:
|
Foreground Service:
LocalDataSource :
Main Isolate:
Don't worry about the HealthDataModel, I don't think this issue is because of the model. |
If it helps, I saw the flutter_foreground_task and they're using PluginUilities like this:
|
I've tried creating a reproduction in this branch: https://github.com/objectbox/objectbox-dart/compare/288-issue-repro but couldn't get the foreground callback to execute - it just logs |
Are you trying this in IOS or Android? It won't work in IOS. I think you're missing some configurations.
|
Thanks, it was the missing permission setting. Some preliminary info: the callback from the foreground task executes in a separate isolate, thus needs to attach to an existing open store, not open a new one. How do you handle this situation? See this for details: https://stackoverflow.com/a/68519353/2386130 |
Okay so the problem is, I'm opening the box twice instead of opening the second box using the reference of the first box? |
You can get the port to the background task (and then send over the store reference) like in this commit: e61ea42 Let me know if that solved your issue. |
Thank you so much, I was gonna give up and move to sqflite, fortunately, this port communication worked and I'm really thankful. Great work and great database. |
Uh oh!
There was an error while loading. Please reload this page.
So I'm running a foreground service using the plugin flutter_foreground_task. Before I was using Hive DB, but since the foreground service creates a new isolate I was not able to access the same box in the main isolate, and other isolates since Hive does not support multi-threading so I tried Object Box. It was working awesome and the main isolate was able to see the changes made in other isolates. But the issue began when I found out that Read operations throw an error when another block accesses(Write/Read) the box at the same time. I read the documentation and it says there can be more than one reader, however it was not the case while I was running the foreground service.
Basic info:
flutter doctor -v
Expected behavior
It was not suppose to throw error since any number of readers are allowed.
Code
Logs, stack traces
Additional context
Add any other context about the problem here.
It tracks your steps even when the app is killed.
Yes, isolates are being used that's why multi-threading is required.
Did you find any workarounds to prevent the issue?
No
The text was updated successfully, but these errors were encountered: