如何创建预先捆绑的领域文件并将数据上传到其中?

Ale*_*ley 2 database realm ios swift

我是 Realm 的新手,我想为我的应用程序提供一个预捆绑的 Realm 文件,但是我不清楚如何实际创建 .realm 文件并将所需的预捆绑数据上传到它的领域文档。我无法通过 SO、Google 或 Youtube 找到任何解决方案。分步说明将非常有帮助。

TiM*_*TiM 5

我们仍在寻找使生成预填充 Realms 更容易的方法。这绝对是 Realm 浏览器所需要的功能,但是由于 Realm 文件在更改架构时需要迁移的方式,因此将其合并到 UI 中有些棘手。这绝对是我们想要改进的地方。

目前,浏览器中有一个转换器,可以执行最少的数据导入,如 CSV 文件。

Realm 文档说,为您的应用程序需要生成预填充 Realm 的最简单方法是构建一个单独的 macOS 应用程序,其唯一作用是生成 Realm 文件,预填充数据,然后公开结果Realm 文件,以便您可以将其复制到您的应用程序。

执行此操作的操作就像任何正常的 Realm 交互(这try! Realm()是导致文件最初被创建的原因),除了您在完成时手动与磁盘上的 Realm 文件交互。

我正在为 iOS 会议开发一个应用程序,该事件的日程安排数据将存储在应用程序发布时与该应用程序预捆绑的 Realm 中。

因为我认为创建一个额外的 macOS 应用程序对于 iOS 应用程序来说是多余的,所以我改为使用 iOS 单元测试,每次执行时都会从头开始生成预捆绑的 Realm。

class Tests: XCTestCase {
    func testGenerateNewDefaultRealm() {
        let sources = [conferences, sponsors, conferenceDays] as [Any]
        XCTAssert(generateDefaultRealm(named: "MyConferenceRealm.realm", sources: sources))
    }
}

extension Tests {
    public func generateDefaultRealm(named name: String, sources: [Any]) -> Bool {
        // Create and configure the Realm file we'll be writing to
        let realm = generateRealm(named: name)

        // Open a Realm write transaction
        realm.beginWrite()

        // Loop through each source and add it to Realm
        for source in sources {
            if let objectArray = source as? [Object] {
                for object in objectArray {
                    realm.add(object)
                }
            }
            else if let objectDictionary = source as? [String : Object] {
                for (_, object) in objectDictionary {
                    realm.add(object)
                }
            }
        }

        // Commit the write transaction
        do {
            try realm.commitWrite()
        }
        catch let error {
            print(error.localizedDescription)
            return false
        }

        // Print the file location of the generated Realm
        print("=====================================================================")
        print(" ")
        print("Successfully generated at")
        print(realm.configuration.fileURL!.path)
        print(" ")
        print("=====================================================================")

        return true
    }

    public func generateRealm(named name: String) -> Realm {
        let exportPath = NSTemporaryDirectory()
        let realmPath = exportPath.appending(name)

        // Delete previous Realm file
        if FileManager.default.fileExists(atPath: realmPath) {
            try! FileManager.default.removeItem(atPath: realmPath)
        }

        // Create new Realm file at path
        let objectTypes: [Object.Type] = [Conference.self, ConferenceDay.self, SessionBlock.self, Event.self, Presentation.self,
                                          Session.self, Location.self, Speaker.self, Sponsor.self, Venue.self]
        let configuration = Realm.Configuration(fileURL: URL(string: realmPath), objectTypes: objectTypes)
        let realm = try! Realm(configuration: configuration)
        return realm
    }
}
Run Code Online (Sandbox Code Playgroud)

运行这个单元测试会NSTemporaryDirectory()在 Mac的目录下生成一个新的 Realm 文件,然后输入一组Array和每次运行测试时创建Dictionary的非持久化 RealmObject实例的对象。然后将最终 Realm 的位置打印在控制台中,以便我可以抓取它并将其移动到应用程序包中。