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

Issue while using isolates #696

Open
Aruljebaraj opened this issue Nov 28, 2024 · 5 comments
Open

Issue while using isolates #696

Aruljebaraj opened this issue Nov 28, 2024 · 5 comments
Labels
documentation Improvements or additions to documentation more info required Needs more info to become actionable. Auto-closed if no response. question How to do something/general question

Comments

@Aruljebaraj
Copy link

Aruljebaraj commented Nov 28, 2024

I’ve implemented a singleton pattern for the ObjectBox class in my Flutter project, and it works correctly when accessed normally. However, when I started implementing isolates, I'm encountering a LateInitializationError.

import 'package:inspectassist_bloc/objectbox.g.dart';

class ObjectBox {
  late final Store store;
  late final Box<Test> testBox;

  ObjectBox._create(this.store) {
    testBox = Box<Test>(store);
  }

  static late final ObjectBox _instance;

  static Future<void> init() async {
    _instance = ObjectBox._create(await openStore());
  }

  static ObjectBox get instance => _instance;
}
LateInitializationError: Field '_instance@85511379' has not been initialized.
#0      ObjectBox._instance (package:inspectassist_bloc/Database/ObjectBox.dart)
#1      ObjectBox.instance (package:inspectassist_bloc/Database/ObjectBox.dart:59:36)

is there any solution to fix this

@Aruljebaraj Aruljebaraj added the bug Something isn't working label Nov 28, 2024
@Aruljebaraj
Copy link
Author

  • tried to initialise it again
  • BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken); // rootToken -- null

@greenrobot-team
Copy link
Member

greenrobot-team commented Dec 2, 2024

Each Dart isolate has its own global fields, they do not share state on the Dart level.

However, as ObjectBox runs on the native or process level (so one native instance shared across all isolates), instead of creating a new Store in another isolate your code should instead attach to the open native store.

Note: I labeled this issue with "more info required" so it will auto-close in a few days if there are no follow-up comments.

@greenrobot-team greenrobot-team added question How to do something/general question more info required Needs more info to become actionable. Auto-closed if no response. and removed bug Something isn't working labels Dec 2, 2024
@github-actions github-actions bot removed the more info required Needs more info to become actionable. Auto-closed if no response. label Dec 3, 2024
@Aruljebaraj
Copy link
Author

Aruljebaraj commented Dec 3, 2024

can I get an example ?
I'm getting BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken); even though I tried store attach

@greenrobot-team
Copy link
Member

Sorry, I can't really help without an error message or code example. But this error message does not sound like it is related to ObjectBox.

Note: I labeled this issue with "more info required" so it will auto-close in a few days if there are no follow-up comments.

@greenrobot-team greenrobot-team added the more info required Needs more info to become actionable. Auto-closed if no response. label Dec 4, 2024
@github-actions github-actions bot removed the more info required Needs more info to become actionable. Auto-closed if no response. label Dec 5, 2024
@greenrobot-team greenrobot-team added the more info required Needs more info to become actionable. Auto-closed if no response. label Dec 9, 2024
@greenrobot-team greenrobot-team removed the more info required Needs more info to become actionable. Auto-closed if no response. label Dec 9, 2024
@greenrobot-team
Copy link
Member

I added notes about this to the Getting Started - Create a Store sections for Flutter and Dart.

@greenrobot-team greenrobot-team added more info required Needs more info to become actionable. Auto-closed if no response. documentation Improvements or additions to documentation labels Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation more info required Needs more info to become actionable. Auto-closed if no response. question How to do something/general question
Projects
None yet
Development

No branches or pull requests

2 participants