How to Preload 'Realm' DB into iOS swift3?

后端 未结 2 1147
盖世英雄少女心
盖世英雄少女心 2021-02-06 17:19

I am trying to implement Realm database in my app , I have realm database with some Preloaded data into it . I have searched through many stack overflow resources but didn\'t ge

相关标签:
2条回答
  • 2021-02-06 17:49

    Select your realm file in Xcode Project Navigator then go open file inspector pane and check Target Membership box for your current target. Your code will work already!

    0 讨论(0)
  • 2021-02-06 17:50

    The piece of code below works perfectly for me to load a preloaded Realm instance during the first launch of my app:

    let defaultPath = Realm.Configuration.defaultConfiguration.fileURL?.path
    let path = Bundle.main.path(forResource: "default", ofType: "realm")
    if !FileManager.default.fileExists(atPath: defaultPath!), let bundledPath = path {
        do {
            try FileManager.default.copyItem(atPath: bundledPath, toPath: defaultPath!)
        } catch {
            print("Error copying pre-populated Realm \(error)")
        }
    }
    
    0 讨论(0)
提交回复
热议问题