android领域预定义的数据库性能

时间:2016-08-23 13:46:50

标签: android realm

我有6236行阿拉伯字符我使用预定义数据库并在此处成功加载它是用于读取文件的代码

  private String copyBundledRealmFile(InputStream inputStream, String outFileName) {
    try {
        File file = new File(this.getFilesDir(), outFileName);
        FileOutputStream outputStream = new FileOutputStream(file);
        byte[] buf = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buf)) > 0) {
            outputStream.write(buf, 0, bytesRead);
        }
        outputStream.close();
        return file.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

并在这里配置领域

       copyBundledRealmFile(SplashScreen.this.getResources().openRawResource(R.raw.tests), "test");


            RealmConfiguration config1 = new RealmConfiguration.Builder(SplashScreen.this)
                .name("test")
                .schemaVersion(1)
                .migration(new Migration())
                .build();
        Realm.setDefaultConfiguration(config1);

并检查一次读取和复制领域

但是每次应用程序打开以进行配置并拥有领域实例时,加载数据的问题大约需要5秒

这是领域实例的代码

    private static MyReleam instance;
private final Realm realm;

public MyReleam(Application application) {
    realm = Realm.getDefaultInstance();
}

public static MyReleam with(Fragment fragment) {

    if (instance == null) {
        instance = new MyReleam(fragment.getActivity().getApplication());
    }
    return instance;
}

public static MyReleam with(Activity activity) {

    if (instance == null) {
        instance = new MyReleam(activity.getApplication());
    }
    return instance;
}

public static MyReleam with(Application application) {

    if (instance == null) {
        instance = new MyReleam(application);
    }
    return instance;
}

public Realm getRealm() {

    return realm;
}

并在此处使用

 this.realm = MyReleam.with(this).getRealm();

如何优化使用它并减少加载时间

2 个答案:

答案 0 :(得分:1)

1。)使用initialData()assetFile()(最好是assetFile())而不是填充迁移

2。)忘记所有你已经阅读in this tutorial,因为它已经过时了(我可以告诉{{1}来自)并参考my article而不是

答案 1 :(得分:0)

感谢EpicPandaForce的回答我使用文档https://realm.io/docs/java/latest/api/io/realm/RealmConfiguration.Builder.html 我是资产传递中的冲突,它只是资产中文件的名称

RealmConfiguration config1 = new RealmConfiguration.Builder(SplashScreen.this).assetFile(SplashScreen.this,"tests")//name of files in assets (test)
                .name("test")
                .schemaVersion(1).migration(new Migration())
                .build();

        Realm.setDefaultConfiguration(config1);