Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom realm file

I'm initializing my realm instance like so:

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

However, I'm gettting this error:

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

All realm has in their swift documentation about creating multiple realms is this example, and even copying it verbatim throws the same error. How do I create and access the realm file?

like image 871
shoe Avatar asked Feb 01 '17 10:02

shoe


2 Answers

Swift 5:

Simply custom your realm file's name

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

custom your realm file's name and path

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

like image 98
dengST30 Avatar answered Oct 06 '22 18:10

dengST30


Bundle.url(forResource:withExtension:) returns the path to an existing file within a given bundle. The portion of the Realm documentation that you link to uses the example of including a Realm file within your app bundle.

Since it looks like you're attempting to create a new Realm file at a specific path, you should compute that path and set it as the fileURL on the Realm.Configuration. You typically do not want to use Bundle APIs for that as files within your app's bundle are not writeable. Instead you should construct a path relative to your app's document or cache directory:

let documentDirectory = FileManager.default.url(for: .documentDirectory, in: .userDomainMask,
                                                appropriateFor: nil, create: false)
let url = documentDirectory.appendingPathComponent("my-new-realm.realm")
like image 37
bdash Avatar answered Oct 06 '22 19:10

bdash