gpt4 book ai didi

realm - 如何使用预填充的 Realm 数据库交付应用程序

转载 作者:行者123 更新时间:2023-12-03 10:01:01 31 4
gpt4 key购买 nike

我想在我的 Realm 数据库中使用已经预填充的数据来交付我的应用程序。我是否必须简单地将它复制到文档目录中,或者还有其他事情要做吗?

最佳答案

Realm 的文档中有一节关于 "Bundling a Realm with an App" :

It’s common to seed an app with initial data, making it available to your users immediately on first launch. Here’s how to do this:

  1. First, populate the realm. You should use the same data model as your final, shipping app to create a realm and populate it with the data you wish to bundle with your app. Since realm files are cross-platform, you can use an OS X app (see our JSONImport example) or your iOS app running in the simulator.

  2. In the code where you’re generating this realm file, you should finish by making a compacted copy of the file (see -[RLMRealm writeCopyToPath:error:]). This will reduce the Realm’s file size, making your final app lighter to download for your users.

  3. Drag the new compacted copy of your realm file to your final app’s Xcode Project Navigator.

  4. Go to your app target’s build phases tab in Xcode and add the realm file to the “Copy Bundle Resources” build phase.

  5. At this point, your bundled realm file will be accessible to your app. You can find its path by using [[NSBundle mainBundle] pathForResource:ofType:].

  6. You can either create a read-only realm by calling [RLMRealm realmWithPath:readOnly:error:]. Or, if you’d like to create a writable realm file based on this initial data, you can copy the bundled file to your application’s Documents directory using [[NSFileManager defaultManager] copyItemAtPath:toPath:error:] and then construct your new realm by using [RLMRealm realmWithPath:].

You can refer to our migration sample app for an example of how to use a bundled realm file.

关于realm - 如何使用预填充的 Realm 数据库交付应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29839740/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com