Kubik
Kubik

Reputation: 610

A RealmObject with no @PrimaryKey cannot be updated

I am currently refactoring some old app and I am trying to migrate from SQLite to Realm. I have used Realm before and I have never encountered problem like this. When I start my app for first time (after installation), I get this exception:

E/AndroidRuntime: FATAL EXCEPTION:
Process: xx.xxx.xxx.beta.realm, PID: 25947
java.lang.IllegalArgumentException: A RealmObject with no @PrimaryKey cannot be updated: class xx.xxx.xxx.realm.Vod
   at io.realm.Realm.checkHasPrimaryKey(Realm.java:1184)
   at io.realm.Realm.copyToRealmOrUpdate(Realm.java:713)
   at xx.xxx.xxx.services.VodService$4.run(VodService.java:232)
   at java.lang.Thread.run(Thread.java:818)

My Vod class looks like this and object is properly created, i.e. has value for PrimaryKey and all other fields:

public class Vod extends RealmObject {

    @PrimaryKey
    private String uuid;

    private String name;
    private Integer lengthMin;
    private Boolean hasTrailer;
    private String description;
    private String originalName;

    //etc...

    //getters & setters

This will crash the app. But after that first time (and every next time) everything works fine - with same code and same data. But when I uninstall and reinstall app again, I will also encounter this exception again. The part of code in question runs on background thread. If I move it to UI thread, everything works fine even for first time. But I want to parse network responses on background thread and not on UI. Also it isn't specific to Vod class, if I skip data for Vod and start with, for example, User class, then I get "no @PrimaryKey" exception for User class.

Sometimes it also throws this exception:

D/REALM: jni: ThrowingException 5, , .
D/REALM: Exception has been throw: File not found: .
E/AndroidRuntime: FATAL EXCEPTION:
   Process: xx.xxx.xxx.beta.realm, PID: 28555
   io.realm.exceptions.RealmIOException: File not found: .
       at io.realm.internal.SharedGroup.createNativeWithImplicitTransactions(Native Method)
       at io.realm.internal.SharedGroup.<init>(SharedGroup.java:67)
       at io.realm.internal.SharedGroupManager.<init>(SharedGroupManager.java:47)
       at io.realm.BaseRealm.<init>(BaseRealm.java:76)
       at io.realm.Realm.<init>(Realm.java:126)
       at io.realm.Realm.createAndValidate(Realm.java:246)
       at io.realm.Realm.createInstance(Realm.java:231)
       at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:114)
       at io.realm.Realm.getDefaultInstance(Realm.java:181)
       at xx.xxx.xx.services.RecordingService$9.run(RecordingService.java:317)
    at java.lang.Thread.run(Thread.java:818) 

which leads me to think, that there will be some problem with initialization of Realm file? I create it like this:

public class MainApplication extends Application {

  private static Context mContext;

  @Override
  public void onCreate() {
    super.onCreate(); 

    mContext = getApplicationContext();

    mRealmConfig = new RealmConfiguration.Builder(mContext).build();
    Realm.setDefaultConfiguration(mRealmConfig);
  }
}

and then use this to get instance in thread:

Realm localRealm = Realm.getDefaultInstance();

Am I doing something wrong? Has anyone has this type of problem before? I tried to search web for answer, but found nothing useful. Thanks for any help.

(I'm using io.realm:realm-android:0.87.4)

EDIT: I have confirmed (via ADB), that internal storage folder for my app package is removed after uninstall. While checking that, I found out that my files folder in path /data/data//files is empty during first app run. Is that correct behavior? Also, I cannot see anything inside Realm via Stetho.

Steps: 1. I uninstall app

  1. ADB says cd: /data/data/xx.xxx.xxx.beta.realm: No such file or directory

  2. I install and run app again

  3. ADB run-as xx.xxx.xxx.beta.realm -> cd files -> ls -> empty

  4. Inspect via Stetho, shows no data

  5. Wait for minute or so (to give Realm time to create whatever needs)

  6. Start network requests and app crashes ("no @PrimaryKey" exception) with first data to parse outside UI thread (parsing on UI works fine)

  7. ADB -> files folder -> ls -> I can see everything, default.realm, default.realm.lock, default.realm.log, default.realm.log_a, default.realm.log_b

  8. Start app again, everything works fine, I can even see realm data via Stetho.

I really hope that I am making some stupid mistake and that everything will be fine at last. Also one more thing, on app start I see this log few times:

Rejecting re-init on previously-failed class java.lang.Class<io.realm.rx.RealmObservableFactory$4>

I have read this: https://github.com/realm/realm-java/issues/1990 and I don't think, that it is related to my problem, but just to be sure.

Upvotes: 5

Views: 3283

Answers (1)

avinash396i
avinash396i

Reputation: 21

It's possible that your imports are messed up. Be sure to be importing io.realm.annotations.PrimaryKey, and not androidx.room.PrimaryKey, which might be a common issue.

Upvotes: 2

Related Questions