Use pre-populated databases with Realm

后端 未结 2 1808
余生分开走
余生分开走 2021-02-15 16:15

How could we convert SQLite database to a Realm database?

Is there any way to use pre-populated databases with Realm on Android?

2条回答
  •  天涯浪人
    2021-02-15 16:57

    This is an answer to the second part of the question.

    While shipping a prepopulated database in the assets folder of your app, analogous to the official Objective-C to bundle of Realm with your app, is still probably the fastest way to get a prepopulated Realm if you have a lot of data. But as of Realm Java 0.89 there is now an official way to prepopulate a Realm database on Android.

    There is now a method that allows for specifying a transaction to be run when a Realm database is created for the first time. You should call this method, initialData(Realm.Transaction transaction), as part of setting up the RealmConfiguration Builder.

    For example

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

提交回复
热议问题