objectbox / objectbox/objectbox-dart

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

Aperta
#653 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

documentation
Lingua principale
Dart
Stelle
1.2k
Fork
162
Merge medio
15m
PR unite (30g)
1

Descrizione

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!

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo il flusso delle notifiche in background su un dispositivo iOS fisico, concentrandoti su createStore(), openStore() e Store.attach() nel gestore in background di Firebase. Confronta il ciclo di vita dello store e il comportamento dell’isolate con Android; il lavoro è completato quando viene identificata la causa dell’UnsupportedError relativo a più store e viene fornita una soluzione verificata o una limitazione documentata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
dart, firebase, flutter
Ambito
database, mobile
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.