简体   繁体   English

Realm 文件用于单元测试路径权限(Xcode,Swift)

[英]Realm file for unit testing path permission (Xcode, Swift)

Please forgive me if there is answered question similar to this.如果有类似的回答问题,请原谅我。 I tried to find any but couldn't find it我试图找到任何但找不到

Hi guys, i am having problem with running Realm file for my unit testing大家好,我在为我的单元测试运行 Realm 文件时遇到问题

So i have moved the realm files to my project test folder所以我已将 realm 文件移动到我的项目测试文件夹中

   SampleAppTest
      + SampleAppTest.swift
      + SampleRealm.realm

I can successfully located the realm file, however, when I tried to instantiate the Realm connection, it threw me an error saying我可以成功找到 realm 文件,但是,当我尝试实例化 Realm 连接时,它给我一个错误说

Please use a path where your app has read-write permissions.

    let bundle = Bundle(for: type(of: self))
    if let path = bundle.path(forResource: "SampleRealm", ofType: "realm") {
        let realmLocationURL = URL(string: path)
        
        let realmVersion: UInt64 = 1
        let config = Realm.Configuration(fileURL: realmLocationURL, schemaVersion: realmVersion, migrationBlock: { migration, oldSchemaVersion in
                if (oldSchemaVersion < realmVersion) {}
            }
        )
        let realm = try! Realm(configuration: config) // throws error
    }
    else {
        print("path not found")
    }

Folder access in Finder -> Get Info -> Read & Write

So in general, where can I put a file that my app has a permission to read-write?所以一般来说,我可以将我的应用程序具有读写权限的文件放在哪里? Is there a possible way to put within the project so that other team member that clone the project can also run the test with same realm file?有没有一种可能的方法可以放入项目中,以便克隆项目的其他团队成员也可以使用相同的 realm 文件运行测试? Thank you谢谢

Storing Realm in your App Bundle makes it a read only realm so I don't think that was the intention.在您的App Bundle中存储 Realm 使其成为只读 realm 所以我认为这不是意图。

The default location Realm uses for your Realm files is always a good bet - however, it won't travel with your project files. Realm 用于您的 Realm 文件的默认位置始终是一个不错的选择 - 但是,它不会与您的项目文件一起移动。

During development, we often store our realm file with the project - that way if the project is compressed, sent or shared (or put it on dropbox) the file is always available and is read/write.在开发过程中,我们经常将 realm 文件与项目一起存储 - 这样,如果项目被压缩、发送或共享(或将其放在保管箱上),该文件始终可用并且可以读取/写入。 Here's some code to do that:这里有一些代码可以做到这一点:

func gGetRealm() -> Realm? {
    do {
        let fileUrl = URL(fileURLWithPath: #file)
        let projectSubUrl = fileUrl.deletingLastPathComponent() //the project files folder
        let projectUrl = projectSubUrl.deletingLastPathComponent() //the project folder
        let realmURL = projectUrl.appendingPathComponent("default.realm")

        var config = Realm.Configuration.defaultConfiguration
        config.fileURL = realmURL
        let realm = try Realm.init(configuration: config)
        return realm

    } catch let error as NSError { //just throw the error to the console
        print("Error!")
        print("  " + error.localizedDescription)
        let err = error.code
        print(err)
        let t = type(of: err)
        print(t)
        return nil
    }
}

This is global function to get the Realm stored within the project file - use is这是全局 function 以获取存储在项目文件中的 Realm - 使用是

guard let realm = gGetRealm() else { return }
let results = realm.objects(MyObject.self)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM