Kero Fawzy Kero Fawzy - 10 days ago 10
Android Question

Realm PrimaryKey Constraint Exception

i had a problem with realm i try to fill realm with my data i got it from json and after parse json and every thing is ready i use it but its crashed my app and this is my code

private void putDataInRealm(ArrayList<Movie> resultObj) {
realm.beginTransaction();
for (Movie item : resultObj) {
movieDb = realm.createObject(MovieDb.class);
movieDb.setMovieID(item.getId());
movieDb.setTitle(item.getTitle());
movieDb.setDate(item.getDate());
movieDb.setOverview(item.getOverview());
movieDb.setRate(item.getRate());
movieDb.setVote(item.getVote());
movieDb.setBackdrop_path(item.getBackdrop());
}
realm.commitTransaction();
}


sure i download realm and i make my configuration like that

RealmConfiguration configuration = new RealmConfiguration.Builder(this).name("Movie_data_base.realm").build();
Realm.setDefaultConfiguration(configuration);


my error is

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.massive.movieapp, PID: 27784
io.realm.exceptions.RealmPrimaryKeyConstraintException: Value already exists:
at io.realm.internal.Table.throwDuplicatePrimaryKeyException(Table.java:675)
at io.realm.internal.Table.addEmptyRow(Table.java:404)
at io.realm.Realm.createObject(Realm.java:696)
at com.massive.movieapp.FragmentForActivity.putDataInRealm(FragmentForActivity.java:135)
at com.massive.movieapp.FragmentForActivity.onPostExcuteCallBack(FragmentForActivity.java:150)
at com.massive.movieapp.Url_cont.onPostExecute(Url_cont.java:128)
at com.massive.movieapp.Url_cont.onPostExecute(Url_cont.java:25)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


my device is nexus 7 api 23

Answer

It's exactly what the error says. You're trying to put an object in the database, but it already has an object with the same value of the @PrimaryKey-annotated field. Primary key is by definition unique - it has to identify and reference exactly one object in the database.

A simple way to fix that case is to create the MovieDB as an unmanaged object, and then use realm.insertOrUpdate method (available from v1.1.0) which will check for existence of an object with the same key, and update it if exists or create a new one if it doesn't:

private void putDataInRealm(ArrayList<Movie> resultObj) {
    realm.beginTransaction();
    for (Movie item : resultObj) {
        MovieDb movieDb = new MovieDb();
        movieDb.setMovieID(item.getId());
        movieDb.setTitle(item.getTitle());
        movieDb.setDate(item.getDate());
        movieDb.setOverview(item.getOverview());
        movieDb.setRate(item.getRate());
        movieDb.setVote(item.getVote());
        movieDb.setBackdrop_path(item.getBackdrop());
        realm.insertOrUpdate(movieDb);
    }
    realm.commitTransaction();
}
Comments