-
Notifications
You must be signed in to change notification settings - Fork 133
[Flutter] openStore()
is not working across multiple FlutterEngines
#436
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
Thanks for reporting! First of all, using multiple Flutter Engines seems to be a valid use case. The API is advertised to be used to have one or more Flutter screens/views in an otherwise non-Flutter app as the note above says. I'm not familiar with how this API works, so the following might not be right: the ObjectBox store is created via FFI and refered to using a native pointer. So it should technically be accessible from anything that runs in the same Dart VM. So to access an open store attach to it using |
@greenrobot-team I could try to use |
@greenrobot-team I had another shot at it and
In the background thread I never end up in the
I think I need a way to check if the native store is open (internally that should check the FFI pointer) and then I could use Store.attach()? It should be possible since the native store throws that state error? I'll be happy to provide a minimal reproducible sample app to pinpoint the problem. |
@greenrobot-team In relation to the other issue I had, I'll try to check if the store is open with that static method. Maybe that fixes this issue? |
@greenrobot-team I got it working using |
I read through the code example and docs from Flutter: there should only exist a single Dart VM for all So Change notifications should happen on any isolate/engine as the native side is handling notifications. E.g. it should be possible to put an object in a background worker which is observed by a watched query in the UI. Edit: let me know if this resolves your original request (and this can be closed). |
@greenrobot-team This does indeed resolve my problem, thank you. And yes I managed to use Closing as working as intended. |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Hello! This is my code to init my store
When my application is not closed, but it is not in the foreground, it generate this error, using Firebase Cloud Messaging on background : When app is closed, it work well. You can learn more about my issue here #451 Thank you! |
@madrojudi I did it like this:
|
Thank you @navaronbracke
Currently, It is working. I will continue testing to see if it is stable. |
Thank you madrojudi I tried you way and it works sometimes. Other times, Saving reference then reading back will throw error: As in our case, we fire background process when users interact with Widget remote views so the current workaround will eventually lead to race condition |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
As an alternative to an open check and then doing either open or attach, see #442 (comment) for a workaround on Android. According to this comment having multiple engines is a more common occurrence than thought (e.g. when deep-linking or opening from a notification), so maybe we should update the docs and maybe even offer API for this. (internal issue |
@greenrobot-team please also mention in the docs how to properly get the default path of the ObjectBox store in Flutter, i.e. import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
Future<void> main() async {
final String storeDirectoryPath = path.join(
(await getApplicationDocumentsDirectory()).path,
Store.defaultDirectoryPath,
);
} |
@techouse Thanks, as mentioned above already have done this. It just wasn't released, yet. objectbox-dart/objectbox/lib/src/native/store.dart Lines 446 to 454 in fe91dbf
Edit: this was included with release 2.0.0. |
ℹ️ Quick links:
(Edit by ObjectBox)
I have a use case where I have a Flutter app that does two things.
void main(){}
entrypoint that runs a regular Flutter app (i.e.runApp(MaterialApp())
)This app uses the database normally. It also schedules tasks using the
workmanager
pluginworkmanager
plugin that executes the tasks that the app schedulesThe problem
The
void main(){}
entrypoint is executed from the default FlutterEngine (created by the Activity/AppDelegate)The workmanager plugin creates a new FlutterEngine for each task it needs to run. This is because the DartExecutor from the original FlutterEngine is executing the void main(){} entrypoint. A DartExecutor can only run one entrypoint at a time.
Because there are two FlutterEngines (each with their own Isolate pool and such), the Dart code is not in sync between the Engines. That is, one engine might have a
Store _myStore
that is not null, but the other engine still has one that is null (because it does not have the same memory pool allocated).This results in the following code failing on the FlutterEngine that didn't open the store:
Describe the solution you'd like
I'd like to be able to use a Store across different FlutterEngines.
If
openStore()
would return a new connection, regardless of the FlutterEngine, that would be sufficient I think. (I.e. using a connection pool)I'd expect the ObjectBox API to work as-is through this connection. I.e. CRUD / observable queries should work on this new connection.
Then I could do something like this to fix my problem:
main.dart
my_app.dart
task_runner.dart
Additional note
This could also benefit the add-to-app use case where people use a FlutterEngine per view they embed into an existing native app.
The text was updated successfully, but these errors were encountered: