objectbox / objectbox/objectbox-dart

Issue with Store.attach() throwing UnsupportedError on iOS physical device

Ouverte
#653 3 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

documentation
Langage dominant
Dart
Étoiles
1.2k
Forks
162
Merge moyen
15 min
PR mergées (30 j)
1

Description

Hello,

I am using the objectbox-dart library in my Flutter application, which also utilizes Firebase Cloud Messaging for background notifications. When handling notifications in the background on a physical iOS device, I encounter an exception when calling the Store.attach() method. This issue does not occur on an Android emulator.

Here is how I use the library:

Future<String> getDatabaseDirectoryPath() async {
  final dir = await getApplicationDocumentsDirectory();
  return '${dir.path}${Platform.pathSeparator}objectbox';
}
static Future<Store> tryOpenStore(String dbPath,
      {required int maxRetries}) async {
    try {
      if (maxRetries > 0) {
        return await openStore(directory: dbPath);
      } else {
        const exceptionMessage =
            "Couldn't open store because of recurring SchemaExceptions.";
        globalLogger.i(exceptionMessage);
        throw Exception(exceptionMessage);
      }
    } on SchemaException catch (_) {
      maxRetries--;
      await deleteDatabaseFiles(dbPath);
      return await tryOpenStore(dbPath, maxRetries: maxRetries);
    }
}
static Future<Store> createStore() async {
  final dbPath = await getDatabaseDirectoryPath();
  final isOpen = Store.isOpen(dbPath);
  if (isOpen) {
    final store = Store.attach(getObjectBoxModel(), dbPath);
    return store;
  } else {
    final store = await tryOpenStore(dbPath, maxRetries: 2);
    return store;
  }
}

The crash occurs in an event triggered when the application is in the background (receiving a Firebase notification):

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // Initialize some stuff here…
  final store = await RubisObjectBox.createStore(); // works on Android, not on iOS
  MessagingHelper.handleMessage(
    DataMessage.fromJson(message.data), message.data, store, runFromBackground: true
  );
}

On iOS, the Store.attach() method throws an UnsupportedError with the message:

Cannot create multiple Store instances for the same directory in the same isolate. Please use a single Store, close() the previous instance before opening another one or attach to it in another isolate.

I tried using Isolate.run to return the store, but it seems not possible since the getApplicationDocumentsDirectory() method cannot be called within an isolate.

Environment:

  • Flutter version: 3.22.2
  • Dart version: 3.4.3
  • objectbox version: ^4.0.1
  • For the Android Platform, I used an Emulator directly on Android Studio (Pixel_3a_API_34_extension_level_7_x86_64)
  • For the iOS Platform, I used a Physical Device (iPhone 11 with iOS version 17.5.1)

Steps to reproduce:

  • Set up a Flutter application using objectbox 4.0.1
  • Create a store with the method openStore()
  • Get a new instance of the same store using attach() without closing the previous store
  • Observe the exception.

Any guidance or solution for this issue would be greatly appreciated. Thank you!

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par reproduire le flux de notifications en arrière-plan sur un appareil iOS physique, en vous concentrant sur createStore(), openStore() et Store.attach() dans le gestionnaire d’arrière-plan Firebase. Comparez le cycle de vie du store et le comportement de l’isolate avec Android ; le travail est considéré comme terminé lorsque la cause de l’UnsupportedError lié à plusieurs stores est identifiée et qu’une résolution vérifiée ou une limitation documentée est fournie.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
dart, firebase, flutter
Domaine
database, mobile
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
38/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.