objectbox / objectbox/objectbox-dart

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

オープン
#653 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

documentation
主要言語
Dart
スター
1.2k
フォーク
162
平均マージ
15分
マージ済み PR(30日)
1

説明

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!

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、物理 iOS デバイス上でバックグラウンド通知のフローを再現し、Firebase のバックグラウンドハンドラーにおける createStore()、openStore()、Store.attach() に焦点を当てます。ストアのライフサイクルと isolate の動作を Android と比較します。複数のストアに関する UnsupportedError の原因が特定され、検証済みの解決策または文書化された制限が提供されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
dart, firebase, flutter
領域
database, mobile
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。