领域Swift捆绑数据 [英] Realm Swift Bundle Data

查看:85
本文介绍了领域Swift捆绑数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个csv文件转换为一个领域文件,我想在我的应用程序中使用它. 这是我的代码atm:

I converted a csv file to a realm file and I want to use it in my app. This is my code atm:

func inLibrarayFolder(fileName: String) -> URL {
    return URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0], isDirectory: true)
        .appendingPathComponent(fileName)
}


func copyPreBundleDataCompeletely() {
    let mainRealmUrl = inLibrarayFolder(fileName: "main.realm")
    let bundleUrl = Bundle.main.url(forResource: "treesFull", withExtension: "realm")!
    
    //After launch after fresh install (if main.realm never created)
    if (!FileManager.default.fileExists(atPath: mainRealmUrl.path)){
        //copy bundled data into writable location compeletely
        try! FileManager.default.copyItem(
            at: bundleUrl, to: mainRealmUrl)
        print(mainRealmUrl)
    }
}

在第一次启动期间,它将创建新文件,但该文件与原始文件有所不同:

During the first launch, it creates the new file, but the file is a bit different from the original:

原始数据库 复制的数据库

树对象:

class Tree: Object {
    @objc dynamic var id: Int32 = 0
    @objc dynamic var br = ""
    @objc dynamic var nm1 = ""
    @objc dynamic var nm2 = ""
    @objc dynamic var nm3 = ""
    @objc dynamic var longitude = 0.0
    @objc dynamic var latitude = 0.0
 
//    override static func primaryKey() -> String? {
//        return "id"
//    }
    
}

新文件中似乎有2个数据库,如何访问第二个数据或如何正确复制文件?

It looks like I have 2 databeses in the new file, how can I access the second one with the data or how can I copy the file properly?

此外,当我将ID设为主键时会发生什么?显然我在原始下载文件中没有这样的参数,所以我想我将需要以某种方式迁移数据...

Also, whats gonna happen when I make the id to a primary key? Obviously I dont have a parameter like that in the original downloaded file, so I guess I will need to migrate the data somehow...

推荐答案

在导入时,要导入的文件必须采用非常特定的格式以及特定的文件名

When it comes to importing, the file being imported has to be in a very specific format along with a specific file name

您的Realm对象名称是 Tree ,因此导入的文件名需要匹配

Your Realm object name is Tree, so the imported file name needs to match

Tree.csv

文件的第一行还必须与类的属性名匹配,以逗号分隔

along with that the first line of the file needs to match the classes property names, comma separated

id,br,nm1...

我建议创建一个非常小的测试文件,以3-4行导入以使其正常工作.然后,一旦您掌握了该文件,就可以导入大文件.

I would suggest creating a very small test file to import with 3-4 lines to get it working. Then, once you mastered that then import the big file.

这篇关于领域Swift捆绑数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆