需要在我的应用程序中提供一些演示数据,它使用realm作为数据库



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

但是如果我存档我的应用程序,当然realm文件是空的。

我该如何"存储"?在我的领域文件中有一些演示数据,以便在我上传的二进制文件中查看?

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

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

然后你可以从捆绑领域中提取数据-它将是只写的

然后您可以像这样访问它,假设您的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")

最新更新