iosMentalist
iosMentalist

Reputation: 3084

Error only one device : open() failed: Operation not permitted Path: default.realm.lock

static func realmConfig() -> Realm.Configuration {

    var config = Realm.Configuration(schemaVersion: 6, migrationBlock: { (migration, oldSchemaVersion) in
        /// Migration block. Useful when you upgrade the schema version.
        
    })
    
    config.fileURL = Bundle.main.url(forResource: "default", withExtension: "realm")!
    print(config.fileURL)
    let folderPath = config.fileURL!.deletingLastPathComponent().path
    let lockPath = Bundle.main.url(forResource: "default.realm", withExtension: "lock")!.path
    do {
        try FileManager.default.setAttributes([FileAttributeKey.protectionKey: FileProtectionType.none],ofItemAtPath: folderPath)
        try FileManager.default.setAttributes([FileAttributeKey.protectionKey: FileProtectionType.none],ofItemAtPath: lockPath)
    } catch {
        fatalError("Realm initialization failed, Error:\(error)")
    }
    
    return config
}

private static func realmInstance() -> Realm {
    do {
        let newRealm = try Realm(configuration: realmConfig())
        return newRealm
    } catch {
        print(error)
        fatalError("Unable to create an instance of Realm")
    }
}

}

Upvotes: 0

Views: 999

Answers (2)

Deepak Singh
Deepak Singh

Reputation: 251

Try to add NSFileProtectionNone on parent directory let realm = try! Realm()

// Get our Realm file's parent directory let folderPath = realm.configuration.fileURL!.deletingLastPathComponent().path

// Disable file protection for this directory try! FileManager.default.setAttributes([FileAttributeKey(rawValue: NSFileProtectionKey): NSFileProtectionNone], ofItemAtPath: folderPath)

Upvotes: 0

Jay
Jay

Reputation: 35667

The realm .lock file is an under the hood file used only by realm. You will never need to work with that file directly or bundle it when using a bundled realm. All that's needed the the realm file itself, dragged into the XCode project.

Here's how to access a bundled Realm called MyBundledData.realm:

let config = Realm.Configuration(
    // Get the URL to the bundled file
    fileURL: Bundle.main.url(forResource: "MyBundledData", withExtension: "realm"),
    // Open the file in read-only mode as application bundles are not writeable
    readOnly: true)

// Open the Realm with the configuration
let realm = try! Realm(configuration: config)

that should be all that's needed

Upvotes: 1

Related Questions