androidAhmed
androidAhmed

Reputation: 237

android realm predefined database performance

I have 6236 rows with Arabic character I used predefined database and load it successfully here is the code for read the file

  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;
}

and make configure for realm here

       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);

and make check to read and copy realm one time

but problem loading the data take about 5 seconds every time the app open to make the configure and have instance of realm

here is the code of realm instance

    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;
}

and use it here

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

how can I optimize using it and decrease time of loading

Upvotes: 0

Views: 354

Answers (2)

androidAhmed
androidAhmed

Reputation: 237

thanks for answer of EpicPandaForce I use documentation as https://realm.io/docs/java/latest/api/io/realm/RealmConfiguration.Builder.html the I was conflict in asset pass it was only the name of files in asset

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);

Upvotes: 0

EpicPandaForce
EpicPandaForce

Reputation: 81539

1.) Use initialData() or assetFile() (preferably assetFile()) instead of populating in a migration

2.) Forget everything you've read in this tutorial because it's an outdated mess (I can tell that's where the MyReleam is from) and refer to my article instead

Upvotes: 1

Related Questions