5

我正在初始化我的领域实例,如下所示:

private static let sUserRealm:Realm = try! Realm(configuration:Realm.Configuration(
        fileURL: Bundle.main.url(forResource: "user" ,withExtension: "realm"),
        inMemoryIdentifier: nil,
        syncConfiguration: nil,
        encryptionKey: nil,
        readOnly: false,
        schemaVersion: 0,
        migrationBlock: sMigrationBlock,
        deleteRealmIfMigrationNeeded: true,
        objectTypes: nil))

但是,我收到了这个错误:

fatal error: A Realm Configuration must specify a path or an in-memory 
identifier.: file /Users/realm/workspace/Package iOS Swift/tightdb_objc/RealmSwift/RealmConfiguration.swift, line 201

所有领域在其关于创建多个领域的快速文档中都有这个示例,即使逐字复制也会引发相同的错误。如何创建和访问领域文件?

4

4 回答 4

7

斯威夫特 5:

只需自定义您的领域文件的名称

struct Kingdom {
    static let persist = Kingdom()

    let realm: Realm = { () -> Realm in
        var config = Realm.Configuration()
        config.fileURL = config.fileURL!.deletingLastPathComponent().appendingPathComponent("deng.realm")
        let land = try! Realm(configuration: config)
        return land
    }()
}

自定义您的领域文件的名称和路径

struct Kingdom {
    static let persist = Kingdom()

    let realm: Realm = { () -> Realm in
        let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        let documentsDirectory = paths[0]
        let docURL = URL(string: documentsDirectory)!
        let dataPath = docURL.appendingPathComponent("Dng")
        if FileManager.default.fileExists(atPath: dataPath.absoluteString) == false{
            do {
                try FileManager.default.createDirectory(atPath: dataPath.absoluteString, withIntermediateDirectories: true, attributes: nil)
            } catch {
                print(error.localizedDescription)
            }
        }
        do {
            var config = Realm.Configuration()
            config.fileURL = dataPath.appendingPathComponent("deng.realm")
            let realmReal = try Realm(configuration: config)
            return realmReal
        } catch {
            print(error.localizedDescription)
            let realmForSyntax = try! Realm()
            return realmForSyntax
        }
    }()
}

111

于 2020-03-06T09:26:30.823 回答
5

Bundle.url(forResource:withExtension:)返回给定包中现有文件的路径。您链接到的领域文档部分使用在您的应用程序包中包含领域文件的示例。

由于看起来您正在尝试在特定路径上创建fileURL的 Realm 文件,因此您应该计算该路径并将其设置为Realm.Configuration. 您通常不想为此使用BundleAPI,因为您的应用程序包中的文件不可写。相反,您应该构建一个相对于您的应用程序的文档或缓存目录的路径:

let documentDirectory = FileManager.default.url(for: .documentDirectory, in: .userDomainMask,
                                                appropriateFor: nil, create: false)
let url = documentDirectory.appendingPathComponent("my-new-realm.realm")
于 2017-02-01T18:45:23.620 回答
1

这是一个示例 Realm 配置类,每个配置创建一个新的 Realm 文件。

     import Foundation
     import RealmSwift

     class RealmConfiguration {

// Create "default.realm" in document file
        public static var defaultDataConfiguration = Realm.Configuration.defaultConfiguration

// Create "insensitive.realm"
        static func insensitiveDataConfiguration() -> Realm.Configuration {
            var config = Realm.Configuration()
            config.fileURL = config.fileURL!.deletingLastPathComponent().appendingPathComponent("insensitive.realm")
            return config
        }

// Create "sensitive.realm"
        static func sensitiveDataConfiguration() -> Realm.Configuration {
            var config = Realm.Configuration()
            config.fileURL = config.fileURL!.deletingLastPathComponent().appendingPathComponent("sensitive.realm")
            return config
        }
    }
于 2018-12-15T15:15:22.353 回答
0

使用 Bundle.main.url(forResource: "user" ,withExtension: "realm")!,但这个值可能是nil因为你的文件不存在

代替 Bundle.main.url(forResource: "user" ,withExtension: "realm")

小心并设置

编辑

要使用预定义的数据库,您可以使用Bundle.

但是,如果您想为您的应用程序使用新数据库,请使用其他路径。不是捆绑。

https://realm.io/docs/swift/latest/

var config = Realm.Configuration()

  // Use the default directory, but replace the filename with the username
  config.fileURL = config.fileURL!.deletingLastPathComponent()
                         .appendingPathComponent("\(username).realm")

sUserRealm:Realm = try! Realm(configuration:config)

或任何你想要但不是的路径Bundle

创建和编辑文件和目录:

let fileManager:FileManager = FileManager.default
        let documentdir:String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!

        let dbName = someDataBaseFileName

        let yourFullFileName = String(format:"%@/%@",documentdir,dbName)

//或者

let yourFullPath:String = URL(fileURLWithPath: documentdir).appendingPathComponent(dbName).path
于 2017-02-01T11:01:28.133 回答