Skip to content

Store can not be opened from a background service when initialized from main thread, neither I am able to check if the store is open on given location. #699

Open
@Chandio763

Description

@Chandio763

I have searched existing issues

  • objectbox version: 4.0.3
  • Flutter/Dart version: 3.24.5
  • Build OS: Windows 11
  • Deployment OS or device: Android 12

Steps to reproduce

  1. Create a sample project or clone flutter_background_service example()
  2. Create a sample class and add objectbox dependencies and generate objectbox code.
  3. Initialize Object Box(openStore) in main method and inside onStart method of a background service, try to access that store. It will be null inside service(Isolates have different memory resources). If We try to open store on same location, it throws Exceptions "Bad state: failed to create store: Cannot open store: another store is still open using the same path". I want to use the same data to I have to access the same store.

### Expected behavior
If I am inside the background service. I should be able to openStore on that location to access data or I can get that opened store to perform operations. I should be able to perform operations on same store from UI thread and background service as well.

Actual behavior:

It throws error stating store is already opened.

Code

Please use example code of flutter background service

Things you maybe should also include:

  • My Entity Class

import 'package:objectbox/objectbox.dart';

@Entity()
class PrayerModel {
  @Id(assignable: true) // Allows manually assigned IDs for predefined prayers.
  int id = 0;
  String prayerName;
  String prayerText;
  String silentStartTime;
  String silentEndTime;
  String reminderTime;
  bool silentSwitchEnabled;
  bool reminderSwitchEnabled;
  PrayerModel({
    required this.id,
    required this.prayerName,
    required this.prayerText,
    required this.silentStartTime,
    required this.silentEndTime,
    required this.reminderTime,
    required this.silentSwitchEnabled,
    required this.reminderSwitchEnabled,
  });
  
  DateTime parseStringTime(String time, {bool forTomorrow = false}) {
  final now = DateTime.now();
  final parts = time.split(':');
  final hour = int.parse(parts[0]);
  final minute = int.parse(parts[1]);

  // Create DateTime for today or tomorrow
  return DateTime(
    now.year,
    now.month,
    now.day + (forTomorrow ? 1 : 0),
    hour,
    minute,
  );
}
  /// Utility to parse time strings back to `DateTime`
  DateTime parseTime(String time) => DateTime.parse(time);

  PrayerModel copyWith({
    int? id,
    String? prayerName,
    String? prayerText,
    String? silentStartTime,
    String? silentEndTime,
    String? reminderTime,
    bool? silentSwitchEnabled,
    bool? reminderSwitchEnabled,
  }) {
    return PrayerModel(
      id: id ?? this.id,
      prayerName: prayerName ?? this.prayerName,
      prayerText: prayerText ?? this.prayerText,
      silentStartTime: silentStartTime ?? this.silentStartTime,
      silentEndTime: silentEndTime ?? this.silentEndTime,
      reminderTime: reminderTime ?? this.reminderTime,
      silentSwitchEnabled: silentSwitchEnabled ?? this.silentSwitchEnabled,
      reminderSwitchEnabled: reminderSwitchEnabled ?? this.reminderSwitchEnabled,
    );
  }

  Map<String, dynamic> toMap() {
    final result = <String, dynamic>{};
  
    result.addAll({'id': id});
    result.addAll({'prayerName': prayerName});
    result.addAll({'prayerText': prayerText});
    result.addAll({'silentStartTime': silentStartTime});
    result.addAll({'silentEndTime': silentEndTime});
    result.addAll({'reminderTime': reminderTime});
    result.addAll({'silentSwitchEnabled': silentSwitchEnabled});
    result.addAll({'reminderSwitchEnabled': reminderSwitchEnabled});
  
    return result;
  }

  factory PrayerModel.fromMap(Map<String, dynamic> map) {
    return PrayerModel(
      id: map['id']?.toInt() ?? 0,
      prayerName: map['prayerName'] ?? '',
      prayerText: map['prayerText'] ?? '',
      silentStartTime: map['silentStartTime'] ?? '',
      silentEndTime: map['silentEndTime'] ?? '',
      reminderTime: map['reminderTime'] ?? '',
      silentSwitchEnabled: map['silentSwitchEnabled'] ?? false,
      reminderSwitchEnabled: map['reminderSwitchEnabled'] ?? false,
    );
  }

  String toJson() => json.encode(toMap());

  factory PrayerModel.fromJson(String source) => PrayerModel.fromMap(json.decode(source));

  @override
  String toString() {
    return 'PrayerModel(id: $id, prayerName: $prayerName, prayerText: $prayerText, silentStartTime: $silentStartTime, silentEndTime: $silentEndTime, reminderTime: $reminderTime, silentSwitchEnabled: $silentSwitchEnabled, reminderSwitchEnabled: $reminderSwitchEnabled)';
  }
}

My pubspec.yaml

name: khushu
description: A new Flutter project.
publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: '>=3.1.0 <4.0.0'

dependencies:
  flutter:
    sdk: flutter
  flutter_native_splash: ^2.4.1
  google_fonts: ^6.2.1
  flutter_svg: ^2.0.10+1
  flutter_qiblah: ^3.0.3
  sensors_plus: ^6.0.0
  permission_handler: ^11.3.1
  geolocator: ^13.0.2
  flutter_advanced_switch:
  provider: ^6.1.2
  geocoding: ^3.0.0
  shared_preferences: ^2.3.0
  flutter_background_service: ^5.0.10
  sound_mode: ^3.0.0
  flutter_local_notifications: ^18.0.1
  flutter_background_service_android: ^6.2.7
  double_tap_to_exit: ^1.0.1
  share_plus: ^10.0.0
  flutter_rating_bar: ^4.0.1
  # launch_review: ^3.0.1
  # android_alarm_manager_plus: ^4.0.4
  sqflite: ^2.3.3+1
  path: ^1.9.0
  syncfusion_flutter_pdf: ^27.2.5
  path_provider: ^2.1.4
  open_file: ^3.3.2
  url_launcher: ^6.3.0
  upgrader: ^11.3.0
  firebase_core: ^3.4.0
  firebase_analytics: ^11.3.0
  firebase_crashlytics: ^4.1.0
  onesignal_flutter: ^5.2.8
  mailer: ^6.1.2
  device_info_plus: ^11.1.1
  location: ^7.0.1
  # device_preview: ^1.2.0
  objectbox: ^4.0.3
  objectbox_flutter_libs:


dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^5.0.0
  objectbox_generator:
  build_runner:

flutter:
  uses-material-design: true

  assets:
    - assets/images/
    - assets/icons/
    - assets/icons/lt.svg
  fonts:
    - family: Poppins
      fonts:
        - asset: assets/fonts/Poppins-Regular.ttf
        - asset: assets/fonts/Poppins-Regular.ttf
          weight: 900

Logs
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: failed to create store: Cannot open store: another store is still open using the same path: "/data/data/com.prayer.khushu/app_flutter/obx-example" (OBX_ERROR code 10001)
E/flutter (17496): #0      ObjectBoxNativeError.throwMapped (package:objectbox/src/native/bindings/helpers.dart:74:9)
E/flutter (17496): #1      throwLatestNativeError (package:objectbox/src/native/bindings/helpers.dart:54:48)
E/flutter (17496): #2      checkObxPtr (package:objectbox/src/native/bindings/helpers.dart:31:5)
E/flutter (17496): #3      Store._checkStorePointer (package:objectbox/src/native/store.dart:451:7)
E/flutter (17496): #4      new Store (package:objectbox/src/native/store.dart:274:7)
E/flutter (17496): #5      openStore (package:khushu/objectbox.g.dart:91:14)
E/flutter (17496): #6      createStore (package:khushu/CommonFunctions/create_store.dart:13:16)
E/flutter (17496): <asynchronous suspension>
E/flutter (17496): #7      onStart (package:khushu/main.dart:207:13)
E/flutter (17496): <asynchronous suspension>
E/flutter (17496): 

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionHow to do something/general question

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions