如何为我的android应用创建带有初始数据的Realm数据库? [英] How can I create a Realm database with initial data for my android app?

查看:194
本文介绍了如何为我的android应用创建带有初始数据的Realm数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Realm为我的android应用程序创建数据库.安装应用程序时,我需要预先填充的数据.当数据库的版本为0(最初默认为0)时,无法将Realm Migration设置为RealmConfiguration的一部分.第一次安装应用程序时如何添加数据?

I am trying to create a database for my android application using Realm. I need to have data that is pre-populated when the app is installed. Setting a Realm Migration as part of the RealmConfiguration does not run when the version of the database is 0 (defaults to 0 initially). How can I add data the first time the application is setup?

推荐答案

Realm Java 0.89引入了一种方法,该方法允许指定首次创建Realm数据库时要运行的事务.此方法 RealmConfiguration.Builder.initialData(Realm.Transaction transaction) ,是设置RealmConfiguration Builder的一部分.

Realm Java 0.89 introduced a method that allows for specifying a transaction to be run when a Realm database is created for the first time. This method, RealmConfiguration.Builder.initialData(Realm.Transaction transaction), is called as part of setting up the RealmConfiguration Builder.

例如

RealmConfiguration config = new RealmConfiguration.Builder(context)
  .name("myrealm.realm")
  .initialData(new MyInitialDataRealmTransaction()), 
  .build();

这篇关于如何为我的android应用创建带有初始数据的Realm数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆