领域数据未显示在物理设备上 [英] Realm data doesn't show up on a physical device

查看:78
本文介绍了领域数据未显示在物理设备上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用realmSwift开发一个应用程序.我想首先在realm db上设置预设数据,然后我希望它在用户打开应用程序时显示并使其可写.我根据有关领域捆绑包的其他问题编写了一些代码.例如领域-将包含初始数据的文件添加到项目中( iOS/Swift)领域fileExists始终为真 然后,我设法在模拟器上做我想做的事,但是在物理设备上不起作用.这是我在AppDelegate上编写的代码

I'm developing an app with realmSwift. I want to set preset data on realm db initially, then I want it to show up when user open the app and to make it writable. I write some code based on other questions about realm bundle. such as Realm - Add file with initial data to project (iOS/Swift) and Realm fileExists is always true Then I managed to do what I want to on simulator but it doesn't work on a physical device. This is the code I wrote on AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


    openRealm()

    let config = Realm.Configuration(

        fileURL: Bundle.main.url(forResource: "default", withExtension: "realm"),

        readOnly: true)

    var realm = try! Realm(configuration: config)


    print(Realm.Configuration.defaultConfiguration.fileURL)

    return true
}

func openRealm() {

    let realm = try! Realm()
    let bundlePath = Bundle.main.path(forResource: "default", ofType: "realm")
    let destPath = Realm.Configuration.defaultConfiguration.fileURL?.path
    let fileManager = FileManager.default

    if fileManager.fileExists(atPath: destPath!) {
        //File exist, do nothing
        print("File exist")

        try! fileManager.removeItem(atPath: destPath!)

        do {
            //Copy file from bundle to Realm default path
            try fileManager.copyItem(atPath: bundlePath!, toPath: destPath!)
            print("Copied")
        } catch {
            print("\n",error)
        }


    } else {
        do {
            try fileManager.copyItem(atPath: bundlePath!, toPath: destPath!)
            print("Copied")
        } catch {
            print("\n",error)
        }
    }



}

推荐答案

您的代码存在3个问题:首先,您不应该使用if分支的true部分,因为只要您运行<从AppDelegate的applicationDidFinishLaunching中调用c1>并在对Realm进行任何调用之前,存在default.realm文件的唯一原因是因为您的应用程序不是第一次启动,所以您不应覆盖它.

There are 3 issue with your code: firstly, you shouldn't be using the true part of your if branch, since as long as you run openRealm() from your AppDelegate's applicationDidFinishLaunching and before you'd make any calls to Realm the only reason why a default.realm file can exist is because your app isn't launching for the first time, so you shouldn't overwrite it.

其次,您不应该在openRealm函数的开头调用let realm = try! Realm(),因为这实际上会创建一个realm,这将比检测是否预先填充的.realm更加困难.文件是否已经复制到defaultPath.实际上,您根本不需要在openRealm()函数中完全调用try! Realm(),除非您想要执行迁移,然后才能从代码中的其他任何位置打开您的Realm.

Secondly, you shouldn't be calling let realm = try! Realm() at the beginning of your openRealm function since that will actually create a realm, which will make harder it than it should be to detect if the prepopulated .realm file was already copied to the defaultPath or not. You don't actually need to call try! Realm() at all in the openRealm() function unless you want to perform a migration before your Realm could be opened from anywhere else in your code.

func openRealm() {
    let bundlePath = Bundle.main.path(forResource: "default", ofType: "realm")!
    let defaultPath = Realm.Configuration.defaultConfiguration.fileURL?.path!
    let fileManager = FileManager.default

    // Only need to copy the prepopulated `.realm` file if it doesn't exist yet
    if !fileManager.fileExists(atPath: defaultPath){
    print("use pre-populated database")
    do {
        try fileManager.copyItem(atPath: bundlePath, toPath: defaultPath)
        print("Copied")
    } catch {
        print(error)
    }
}

最后,您不应该创建新的配置来用bundlePath覆盖Realm的路径,因为不应修改应用程序捆绑包中的文件(这将破坏代码签名).此外,您已经将预填充文件从应用程序捆绑包中复制到了Realm的defaultPath中,因此,如果您简单地调用Realm(),则它将修改预填充文件,因为它存储在默认位置.

Lastly, you shouldn't be creating a new configuration where you overwrite the path of Realm with your bundlePath since files in the application bundle shouldn't be modified ever (that will break your code signature). Moreover, you already copied your prepopulated file from your application bundle to Realm's defaultPath, so if you simply call Realm(), it will be modifying the prepopulated file, since it is stored at the default location.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    openRealm()
    print(Realm.Configuration.defaultConfiguration.fileURL)
    return true
}

这篇关于领域数据未显示在物理设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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