如何将预先填充的Default.realm文件放到设备上?

web*_*ets 2 realm ios swift

我有一个realm文件已经填充了在设备上加载应用程序时需要存在的数据.

我该怎么做才能将realm文件放到我的设备上进行测试,如果有人从应用程序商店下载应用程序,我需要做些什么才能确保它已存在?

我正在使用Swift.

yos*_*osh 7

将数据库文件添加到Xcode项目,即"preloaded.realm"确保在首次删除文件时选择添加到目标 添加到目标

然后(从迁移示例中获取)您可以执行类似这样的操作,将预加载的文件复制到默认目录.这将创建一个读/写领域

// copy over old data files for migration
let defaultPath = RLMRealm.defaultRealmPath()
let defaultParentPath = defaultPath.stringByDeletingLastPathComponent

let v0Path = NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("preloaded.realm")
NSFileManager.defaultManager().removeItemAtPath(defaultPath, error: nil)
NSFileManager.defaultManager().copyItemAtPath(v0Path, toPath: defaultPath, error: nil)
Run Code Online (Sandbox Code Playgroud)

这是一般代码的链接https://github.com/realm/realm-cocoa/blob/master/examples/ios/swift-2.2/Migration/AppDelegate.swift