realm / realm-dart

Realm is a mobile database: a replacement for SQLite & ORMs.
Apache License 2.0
773 stars 86 forks source link

realm init bug #1809

Open zhengxiaozhu opened 1 day ago

zhengxiaozhu commented 1 day ago

What happened?

When I initialize realm。 My code looks like this final config = Configuration.local([ xxx.schema, ]); _realm = Realm(config); When I don't specify a path,It works on the emulator and on the iphone.But on Android phones, you get an error。This error is random,The odds are about 80 percent。Errors are as follows。


[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: RealmException: Failed to open file at path './default.realm.lock': Read-only file system. Error code: realm_errno.RLM_ERR_FILE_PERMISSION_DENIED.

0 _raiseLastError. (package:realm_dart/src/handles/native/error_handling.dart:59)

                                                                                                #1      using (package:ffi/src/arena.dart:124)
                                                                                                #2      _raiseLastError (package:realm_dart/src/handles/native/error_handling.dart:48)
                                                                                                #3      PointerEx.raiseLastErrorIfNull (package:realm_dart/src/handles/native/error_handling.dart:16)
                                                                                                #4      new RealmHandle.open (package:realm_dart/src/handles/native/realm_handle.dart:48)
                                                                                                #5      Realm._openRealm (package:realm_dart/src/realm_class.dart:153)
                                                                                                #6      new Realm._ (package:realm_dart/src/realm_class.dart:119)
                                                                                                #7      new Realm (package:realm_dart/src/realm_class.dart:117)
                                                                                                #8      RealmService.init (package:xxx/utils/RealmService.dart:54)
                                                                                                #9      main (package:xxxx/main.dart:24)
                                                                                                #10     _runMain.<anonymous closure> (dart:ui/hooks.dart:301)
                                                                                                #11     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297)
                                                                                                #12     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184)

It is strange that,Call the following code before I initialize it, and the error will disappear。

await getApplicationDocumentsDirectory();

Repro steps

1. Future main() async { WidgetsFlutterBinding.ensureInitialized(); await RealmService.instance.init(); runApp(const MyApp()); }

2. class RealmService{

RealmService._privateConstructor();

static final RealmService _instance = RealmService._privateConstructor();

static RealmService get instance => _instance;

late Realm _realm;

Realm get realm => _realm;

Future init() async { final config = Configuration.local([ xxxx.schema ]); _realm = Realm(config); }

void close() { _realm.close(); }

}

Version

3.24

What Atlas Services are you using?

-- select --

What type of application is this?

Flutter Application

Client OS and version

android

Code snippets

No response

Stacktrace of the exception/crash you're getting

No response

Relevant log output

No response

nirinchev commented 1 day ago

Can you try to specify an explicit path for the config on Android? Seems like for some reason it gets resolved to the root of the filesystem and naturally, your app doesn't have permissions to write there.

zhengxiaozhu commented 1 day ago

Can you try to specify an explicit path for the config on Android? Seems like for some reason it gets resolved to the root of the filesystem and naturally, your app doesn't have permissions to write there.

If I specify the default path, does it mean that the data in the user's phone will be lost? Thank you for your reply 😁