realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.3k stars 2.14k forks source link

Can't create Realm DB file #8052

Closed bezpalyi closed 1 year ago

bezpalyi commented 1 year ago

How frequently does the bug occur?

All the time

Description

My code is:

    do {
        let fileURL = FileManager.default
            .containerURL(forSecurityApplicationGroupIdentifier: "my.app.bundle.id")!
            .appendingPathComponent("default.realm")
        let realmUrl = fileURL.appendingPathComponent(path)
        var config = Realm.Configuration(fileURL: realmUrl)
        config.schemaVersion = 10
        Realm.Configuration.defaultConfiguration = config
        let realm = try Realm()
        return realm
    } catch {
        return nil
    }

This always return nil, I write the error description in Stacktrace & log output

Stacktrace & log output

Error Domain=io.realm Code=5 "Directory at path '/Users/iosdev/Library/Developer/CoreSimulator/Devices/AE6AAC4A-6494-417D-BED8-2A9EEEF448A2/data/Containers/Shared/AppGroup/0E73729C-85A4-412F-9051-D5DA7A88185B/default.realm/auth.realm.lock' does not exist." UserInfo={Error Code=5, NSFilePath=/Users/iosdev/Library/Developer/CoreSimulator/Devices/AE6AAC4A-6494-417D-BED8-2A9EEEF448A2/data/Containers/Shared/AppGroup/0E73729C-85A4-412F-9051-D5DA7A88185B/default.realm/auth.realm.lock, Underlying=open("/Users/iosdev/Library/Developer/CoreSimulator/Devices/AE6AAC4A-6494-417D-BED8-2A9EEEF448A2/data/Containers/Shared/AppGroup/0E73729C-85A4-412F-9051-D5DA7A88185B/default.realm/auth.realm.lock") failed: No such file or directory Path: /Users/iosdev/Library/Developer/CoreSimulator/Devices/AE6AAC4A-6494-417D-BED8-2A9EEEF448A2/data/Containers/Shared/AppGroup/0E73729C-85A4-412F-9051-D5DA7A88185B/default.realm/auth.realm.lock
Exception backtrace:
0   Analytics Insta                     0x000000010b68646b _ZN5realm4util6detail26ExceptionWithBacktraceBaseC2Ev + 43
1   Analytics Insta                     0x000000010b694a8a _ZN5realm4util22ExceptionWithBacktraceISt13runtime_errorEC2IJRKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEEEEEDpOT_ + 58
2   Analytics Insta                     0x000000010b694875 _ZN5realm4util4File11AccessErrorC2ERKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEESB_ + 37
3   Analytics Insta                     0x000000010bf612d9 _ZN5realm4util4File8NotFoundC2ERKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEESB_ + 41
4   Analytics Insta                     0x000000010bf5b2e5 _ZN5realm4util4File8NotFoundC1ERKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEESB_ + 37
5   Analytics Insta                     0x000000010bf5c532 _ZN5realm4util4File13open_internalERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEENS1_10AccessModeENS1_10CreateModeEiPb + 882
6   Analytics Insta                     0x000000010b68ecb6 _ZN5realm4util4File4openERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEENS1_10AccessModeENS1_10CreateModeEi + 54
7   Analytics Insta                     0x000000010b744f42 _ZN5realm2DB4openERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEbRKNS_9DBOptionsE + 1090
8   Analytics Insta                     0x000000010b7498b1 _ZN5realm2DB4openERNS_11ReplicationERKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEERKNS_9DBOptionsE + 161
9   Analytics Insta                     0x000000010b750217 _ZN5realm2DB6createENSt3__110unique_ptrINS_11ReplicationENS1_14default_deleteIS3_EEEERKNS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEERKNS_9DBOptionsE + 295
10  Analytics Insta                     0x000000010b91d378 _ZN5realm5_impl16RealmCoordinator7open_dbEv + 1464
11  Analytics Insta                     0x000000010b91ebd3 _ZN5realm5_impl16RealmCoordinator12do_get_realmENS_11RealmConfigERNSt3__110shared_ptrINS_5RealmEEENS3_8optionalINS_9VersionIDEEERNS_4util17CheckedUniqueLockE + 83
12  Analytics Insta                     0x000000010b91ea03 _ZN5realm5_impl16RealmCoordinator9get_realmENS_11RealmConfigENSt3__18optionalINS_9VersionIDEEE + 899
13  Analytics Insta                     0x000000010ba81da8 _ZN5realm5Realm16get_shared_realmENS_11RealmConfigE + 216
14  Analytics Insta                     0x000000010b5e93d3 +[RLMRealm realmWithConfiguration:queue:error:] + 995
15  Analytics Insta                     0x000000010c1d9ff2 $sSo8RLMRealmC13configuration5queueABSo0A13ConfigurationC_So012OS_dispatch_C0CSgtKcfCTO + 146
16  Analytics Insta                     0x000000010c29bc4f $s10RealmSwift0A0V5queueACSo012OS_dispatch_C0CSg_tKcfC + 127
17  Analytics Insta                     0x000000010b1ffa20 $s15Analytics_Insta20WidgetSharedResourceC8getRealm4path0G5Swift0G0VSgSS_tF + 1296
18  Analytics Insta                     0x000000010b1ff4ed $s15Analytics_Insta20WidgetSharedResourceC8getRealm4type0G5Swift0G0VSgAC0G4TypeO_tF + 61
19  Analytics Insta                     0x000000010b1ffbff $s15Analytics_Insta20WidgetSharedResourceC14addToRealmAuth9realmUseryAA0hK0C_tF + 95
20  Analytics Insta                     0x000000010b19eb2c $s15Analytics_Insta0B8ResourceC15initCurrentUser4compyys6ResultOySbs5Error_pGc_tFyAGy10Swiftagram0F0VsAH_pGcfU_yyScMYccfU_ + 540
21  Analytics Insta                     0x000000010b11da98 $sIeg_IeyB_TR + 40
22  libdispatch.dylib                   0x0000000117c4cd18 _dispatch_call_block_and_release + 12
23  libdispatch.dylib                   0x0000000117c4df5b _dispatch_client_callout + 8
24  libdispatch.dylib                   0x0000000117c5ed55 _dispatch_main_queue_drain + 1463
25  libdispatch.dylib                   0x0000000117c5e790 _dispatch_main_queue_callback_4CF + 31
26  CoreFoundation                      0x00007ff800386b6f __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
27  CoreFoundation                      0x00007ff800381486 __CFRunLoopRun + 2482
28  CoreFoundation                      0x00007ff8003806f7 CFRunLoopRunSpecific + 560
29  GraphicsServices                    0x00007ff809c5c28a GSEventRunModal + 139
30  UIKitCore                           0x000000011db7462b -[UIApplication _run] + 994
31  UIKitCore                           0x000000011db79547 UIApplicationMain + 123
32  Analytics Insta                     0x000000010b29c9ef main + 63
33  dyld                                0x0000000116f522bf start_sim + 10
34  ???                                 0x000000011b651310 0x0 + 4754576144, NSLocalizedDescription=Directory at path '/Users/iosdev/Library/Developer/CoreSimulator/Devices/AE6AAC4A-6494-417D-BED8-2A9EEEF448A2/data/Containers/Shared/AppGroup/0E73729C-85A4-412F-9051-D5DA7A88185B/default.realm/auth.realm.lock' does not exist.}

Can you reproduce the bug?

Yes, always

Reproduction Steps

No response

Version

10.33.0 and 10.29.0

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

MacOS: 13.0.1 (22A400)

Build environment

Xcode version: 14.1 (14B47b) Dependency manager and version: I use Swift Package Manager

dianaafanador3 commented 1 year ago

@bezpalyi In the code I cannot see the value to the path you are appending to your file url and this may be why you are getting an error, because is not a directory path "default.realm/auth.realm.lock". Any reason you want your path to be something like this `'/Users/iosdev/Library/Developer/CoreSimulator/Devices/AE6AAC4A-6494-417D-BED8-2A9EEEF448A2/data/Containers/Shared/AppGroup/0E73729C-85A4-412F-9051-D5DA7A88185B/default.realm/auth.realm.lock

bezpalyi commented 1 year ago

Hi, @dianaafanador3 path is auth.realm in the above code.

let realmUrl = fileURL.appendingPathComponent(path)

dianaafanador3 commented 1 year ago

Sorry I didn't ask before, why are you appending both "default.realm" and "auth.realm" to your file url. Any use cases for this, this is the reason why you may be getting an error. you need to use "default.realm" or "auth.realm"

bezpalyi commented 1 year ago

Actually there are several realm dababase in my app: auth.realm, user.realm etc. So I put all db files into default.realm directory.

dianaafanador3 commented 1 year ago

You may want to create the folder before opening the realm Something like this

do {
        let fileURL = FileManager.default
            .containerURL(forSecurityApplicationGroupIdentifier: "my.app.bundle.id")!
            .appendingPathComponent("default.realm")
        let realmUrl = fileURL.appendingPathComponent(path)
        try fileManager.createDirectory(atPath: fileURL.path, withIntermediateDirectories: true, attributes: nil)
        var config = Realm.Configuration(fileURL: realmUrl)
        config.schemaVersion = 10
        Realm.Configuration.defaultConfiguration = config
        let realm = try Realm()
        return realm
    } catch {
        return nil
    }
bezpalyi commented 1 year ago

@dianaafanador3 It works well, thanks so much.