Create Multiple Model Realm

public boolean synceTransaction(final List<T> list){

    if(list.size() == 0) return false;

    Realm.init(getContext());

    realm = Realm.getInstance(getConfiguration());

    realm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            for(int i = 0;i<list.size();i++) {
                realm.insert(list.get(i));
            }
        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            Log.d("Status", "Successfully");
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            Log.d("Error", error.getMessage());
        }
    });

    return true;
}

//set this configuration to allow create multiple realm model in android
public RealmConfiguration getConfiguration() {
    return new RealmConfiguration
            .Builder()
            .name("transaction-realm")
            .schemaVersion(10)
            .deleteRealmIfMigrationNeeded()
            .build();
}

public void setConfiguration(RealmConfiguration configuration) {
    this.configuration = configuration;
}
Advertisements
This entry was posted in Realm. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s