【问题标题】:need to provide some demo data in my app which uses realm as a database需要在我的应用程序中提供一些演示数据,它使用领域作为数据库
【发布时间】:2021-08-19 16:55:19
【问题描述】:

对于应用审核,我需要在我的应用中提供一些演示数据。

但如果我归档我的应用程序,领域文件当然是空的。

如何在我的领域文件中“存储”一些演示数据,以便在我上传的二进制文件中可用以供审核?

【问题讨论】:

  • 如果你需要一些简单的数据,你可以使用本地数据库的SQLite,或者如果你想要动态数据,你可以使用parse-server。集成和使用非常简单。此外,back4app 是免费的 :)

标签: swift realm


【解决方案1】:

超级简单 - 只需在应用中捆绑一个 Realm 文件!

使用您想要的任何方式(Realm Studio 或使用应用程序写入数据)创建文件,然后将创建的 Realm 文件拖到 XCode 中的项目导航器中。

然后您可以从 Bundled 领域中提取数据 - 它将是只写的

假设您的 Realm 文件名为 MyBundledData.realm,您可以像这样访问它:

let config = Realm.Configuration(
    // Get the URL to the bundled file
    fileURL: Bundle.main.url(forResource: "MyBundledData", withExtension: "realm"),
    // Open in read-only mode as application bundles are not writeable
    readOnly: true)

// Open the Realm with the configuration
let realm = try! Realm(configuration: config)

// Read some data from the bundled Realm
let results = realm.objects(Dog.self).filter("age > 5")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 2016-01-12
    相关资源
    最近更新 更多