0

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

Kero Fawzy
  • 690
  • 1
  • 7
  • 19

2 Answers2

4

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();
}
maciekjanusz
  • 4,702
  • 25
  • 36
  • when i put realm.insertOrUpdate(movieDb); android studio say cant resolve this method – Kero Fawzy Nov 24 '16 at 22:08
  • 2
    What version of realm are you using? Is it the latest one? It has to be AT LEAST 1.1.0 The method IS there https://realm.io/docs/java/2.2.0/api/ Or maybe MovieDb is not extending RealmObject or RealmModel? – maciekjanusz Nov 24 '16 at 22:11
  • MovieDb is extending RealmObject and my realm is io.realm:realm-android:0.84.1 – Kero Fawzy Nov 25 '16 at 14:09
  • could you send for me latest version from realm – Kero Fawzy Nov 25 '16 at 14:50
  • The latest version docs are available here https://realm.io/docs/java/latest/ although updating isn't so simple due to a bunch of breaking changes on the way. You should probably try to update to 0.88.3., which also uses the Gradle plugin solution rather than just `compile 'io.realm...`. – EpicPandaForce Nov 25 '16 at 17:11
  • Its true there are major breaking changes throughout Realm releases, but from the look of the question it seems like this is some learning/experimental/early work so there's probably no important reason to not update and refactor. I'd strongly suggest starting off with the newest version (2.2.0). – maciekjanusz Nov 25 '16 at 17:36
2

That's because you should specify the ID on the creation of the RealmObject. (available since 0.90.0)

for (Movie item : resultObj) {
    movieDb = realm.createObject(MovieDb.class, item.getId());
    //movieDb.setMovieID(item.getId());

Or you could use unmanaged object and insert it (since 1.1.0)

for (Movie item : resultObj) {
    movieDb = new MovieDb();
    movieDb.setMovieID(item.getId());
   ... 
    realm.insertOrUpdate(movieDb);

But in older times, you would do the following

for (Movie item : resultObj) {
    movieDb = new MovieDb();
    movieDb.setMovieID(item.getId());
   ... 
    realm.copyToRealmOrUpdate(movieDb);
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • your why dose not work it give error i cant any thing beside MovieDb.class – Kero Fawzy Nov 25 '16 at 15:18
  • 1
    Then you should update your Realm version to at least 1.1.1 one day, because this functionality was added in 0.90.0. Which was 1.5 years ago. – EpicPandaForce Nov 25 '16 at 17:05
  • 1
    Here is an upgrade guide http://stackoverflow.com/questions/39971209/upgrade-realm-in-an-android-project/39974275#39974275 – EpicPandaForce Nov 25 '16 at 17:12
  • please tell me how to put latest version of realm in my android studio i knew that stupid question but i need the answer – Kero Fawzy Nov 25 '16 at 20:24
  • 1
    As mentioned by the [**official documentation**](https://realm.io/docs/java/latest/), you need to add this to the project level build gradle: `classpath "io.realm:realm-gradle-plugin:1.1.1"` and add this to the module level build gradle: `apply plugin: 'realm-android'` – EpicPandaForce Nov 25 '16 at 21:56
  • It does, you just need to handle the breaking changes. – EpicPandaForce Nov 26 '16 at 11:00