xcode Realm - 将带有初始数据的文件添加到项目(iOS/Swift)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30476179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Realm - Add file with initial data to project (iOS/Swift)
提问by Max
I'm developing an application for iOS using swift and chose Realm as a database solution for it. I wrote default data in AppDelegate using write/add function from realm docs and it works just fine. So after first launch I have a *.realm file with my initial data. In Realm documentation I found a section called "Bundling a Realm with an App", I add my *.realm file to project and to Build Phases as it written.
我正在使用 swift 为 iOS 开发一个应用程序,并选择 Realm 作为它的数据库解决方案。我使用领域文档中的 write/add 函数在 AppDelegate 中编写了默认数据,它工作得很好。所以在第一次启动后,我有一个带有初始数据的 *.realm 文件。在 Realm 文档中,我找到了一个名为“Bundling a Realm with an App”的部分,我将我的 *.realm 文件添加到 project 和 Build Phases 中。
And I can't understand what I should do next (and part about compressing a *.realm file). I've tried to understand a code from Migration Examplebut I don't know Obj-C well.
而且我不明白接下来我应该做什么(以及关于压缩 *.realm 文件的部分)。我试图理解迁移示例中的代码,但我不太了解 Obj-C。
Please give as clear steps as you can to add *.realm file with initial data to swift ios project and load this data to the Realm db with the first launch.
请给出尽可能明确的步骤,将带有初始数据的 *.realm 文件添加到 swift ios 项目,并在首次启动时将此数据加载到 Realm 数据库。
回答by pteofil
Implement this function openRealm
in AppDelegate and call it in
openRealm
在 AppDelegate 中实现这个函数并调用
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
openRealm()
return true
}
func openRealm() {
let defaultRealmPath = Realm.defaultPath
let bundleReamPath = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent("default.realm")
if !NSFileManager.defaultManager().fileExistsAtPath(defaultRealmPath) {
NSFileManager.defaultManager().copyItemAtPath(bundleReamPath!, toPath: defaultRealmPath, error: nil)
}
}
It will copy your realm file that you bundled in the app to the default realm path, if it doesn't exist already. After that you use Realm normally like you used before.
如果它不存在,它会将您捆绑在应用程序中的领域文件复制到默认领域路径。之后,您可以像以前一样正常使用 Realm。
There's also the Migration example that you talked about in Swift.
还有您在Swift 中谈到的 Migration 示例。
In Swift 3.0.1 you may prefer this:
在 Swift 3.0.1 中,你可能更喜欢这个:
let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!
let bundleRealmPath = Bundle.main.url(forResource: "seeds", withExtension: "realm")
if !FileManager.default.fileExists(atPath: defaultRealmPath.absoluteString) {
do {
try FileManager.default.copyItem(at: bundleRealmPath!, to: defaultRealmPath)
} catch let error {
print("error copying seeds: \(error)")
}
}
(but please be careful with the optionals)
(但请注意选项)
回答by Apocal
Swift version 3, courtesy of Kishikawa Katsumi:
Swift 版本 3,由Kishikawa Katsumi 提供:
let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!
let bundleReamPath = Bundle.main.path(forResource: "default", ofType:"realm")
if !FileManager.default.fileExists(atPath: defaultRealmPath.path) {
do
{
try FileManager.default.copyItem(atPath: bundleReamPath!, toPath: defaultRealmPath.path)
}
catch let error as NSError {
// Catch fires here, with an NSError being thrown
print("error occurred, here are the details:\n \(error)")
}
}
回答by ninjaneer
And for those that need @pteofil's answer in Objective-c
对于那些需要@pteofil 在 Objective-c 中给出答案的人
- (void)openRealm {
NSString *defaultRealmPath = [RLMRealm defaultRealm].path;
NSString *bundleRealmPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"default.realm"];
if(![[NSFileManager defaultManager] fileExistsAtPath:defaultRealmPath]) {
[[NSFileManager defaultManager] copyItemAtPath:bundleRealmPath toPath:defaultRealmPath error:nil];
}
}
回答by alias235
Updating @pteofil's openRealm function for Swift 2.2/Realm 1.0.2:
为 Swift 2.2/Realm 1.0.2 更新 @pteofil 的 openRealm 函数:
func openRealm() {
let defaultURL = Realm.Configuration.defaultConfiguration.fileURL!
let bundleReamPath = NSBundle.mainBundle().URLForResource("default", withExtension: "realm")
if !NSFileManager.defaultManager().fileExistsAtPath(defaultURL.path!) {
do {
try NSFileManager.defaultManager().copyItemAtURL(bundleReamPath!, toURL: defaultURL)
}
catch {}
}
}
回答by mac
Work in the enterprise space, I need to open a Realm for each application without reusing Realm across all applications so I put this together for Swift 3.0. Add this function to the AppDelegate.
在企业领域工作,我需要为每个应用程序打开一个 Realm,而不需要在所有应用程序中重用 Realm,所以我将它放在一起用于 Swift 3.0。将此函数添加到 AppDelegate。
func openRealm()
{
let appName = "ApplcationNameGoesHere"
var rlmConfig = Realm.Configuration()
let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!
let appRealmPath = defaultRealmPath.deletingLastPathComponent().appendingPathComponent("\(appName).realm")
if !FileManager.default.fileExists(atPath: appRealmPath.path) {
// Use the default directory, but replace the filename with the application name: appName
rlmConfig.fileURL = rlmConfig.fileURL!.deletingLastPathComponent().appendingPathComponent("\(appName).realm")
}else
{
rlmConfig.fileURL = appRealmPath
}
// Set this as the configuration used for the default Realm
Realm.Configuration.defaultConfiguration = rlmConfig
}// open the Realm database for the application
The code above opens or creates a Realm with the file name of "ApplicationNameGoesHere.realm" based on the appName variable in this example.
上面的代码基于本例中的 appName 变量打开或创建了一个文件名为“ApplicationNameGoesHere.realm”的 Realm。
place
地方
openRealm() before return true in application: didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
openRealm()
return true
}
}
call it in another class like this:
在另一个类中调用它,如下所示:
let uiRealm = try! Realm()
回答by Harry Bloom
回答by Sush Mit
Download Realm Studio in your system. Then print the path from Xcode and copy it:
在您的系统中下载 Realm Studio。然后从 Xcode 打印路径并复制它:
print(Realm.Configuration.defaultConfiguration.fileURL!)
Then open the terminal and write:
然后打开终端写:
open //file path
It will open the file in Realm Studio and you can see your model data there.
它将在 Realm Studio 中打开文件,您可以在那里看到您的模型数据。