Realm with pre populated data into assets?

前端 未结 5 2082
借酒劲吻你
借酒劲吻你 2020-11-29 08:32

Normally I use Realm as:

RealmConfiguration config = new RealmConfiguration.Builder(applicationContext).deleteRealmIfMigrationNeeded().build();
5条回答
  •  被撕碎了的回忆
    2020-11-29 09:16

    Realm has a special parameter in its RealmConfiguration.Builder called assetFile. You could use it like:

    realmConfiguration = new RealmConfiguration.Builder()
                        .assetFile("dataBase/default.realm") // your app's packaged DB
                        ...
                        .build();
    

    just set yer assets DB path and file name and you are good to go without any android-realm-asset-helper lib or copy-file-from-assets code. In this example my app packaged DB-file lies in "assets/dataBase/default.realm".
    Note, version below 2 has a bit another way to call assetFile, you should pass context additionally:

    realmConfiguration = new RealmConfiguration.Builder(context)
            .assetFile(context, "dataBase/default.realm")
            .build();
    

提交回复
热议问题