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")
}
}
}
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