realm-java: Caused by: java.io.NotSerializableException
Hi there,
Since I’ve upgraded Realm from 0.82 to 0.84 (and also to 0.85, 0.86) I started getting this exception. I’ve that Realm Object in a Fragment and when I push the App to background with Home button, App Crashes.
Here is the Full Stack Trace.
Caused by: java.io.NotSerializableException: io.realm.PrinterRealmProxy$PrinterColumnInfo at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344) at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461) at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959) at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360) at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054) at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384) at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461) at android.os.Parcel.writeSerializable(Parcel.java:1442) at android.os.Parcel.writeValue(Parcel.java:1395) at android.os.Parcel.writeArrayMapInternal(Parcel.java:665) at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330) at android.os.Bundle.writeToParcel(Bundle.java:1079) at android.os.Parcel.writeBundle(Parcel.java:690) at android.app.FragmentState.writeToParcel(Fragment.java:140) at android.os.Parcel.writeTypedArray(Parcel.java:1233) at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:380) at android.os.Parcel.writeParcelable(Parcel.java:1416) at android.os.Parcel.writeValue(Parcel.java:1322) at android.os.Parcel.writeArrayMapInternal(Parcel.java:665) at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330) at android.os.Bundle.writeToParcel(Bundle.java:1079) at android.os.Parcel.writeBundle(Parcel.java:690) at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3152) at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3417) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417)
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 1
- Comments: 18 (6 by maintainers)
I would like to point out that I’m successfully passing an object implementing RealmModel and Serializable as an argument from one fragment to another (which didn’t use to happen before the realmmodel interface)
However I do get the “RuntimeException: Parcelable encountered IOException writing serializable object” / “Caused by: java.io.NotSerializableException” which happens the moment i press home/recent apps key. I know how to solve the issue (using a lookup key instead of the object) however, I’m wondering how come it DOES get serialized when i pass it around, yet fails when android tries to saveState…? @cmelchior
Persisted realmObjects are really not intended to be serialised, and I am guessing that it was mostly by accident it worked before.
Between 0.82.1 and 0.86 we changed some of the internals of our Proxy classes and I expect those changes are incompatible with how you serialize the data.
We have added functionality in 0.87.0 that allows you to get a copy of the Realm data (effectively serializing it) using
realm.copyFromRealm()
which I think will suit your use case . Until that is released the work-around would be to copy the data you want manually into a standalone object.@osayilgan Are you saving your RealmObject in a Parcel or Bundle? It looks like Android is trying to write it to disk which fails. One work-around is to save the PrimaryKey in the Parcel instead and then query for the object if needed.
If that is not the case. Can you post the content of your Fragment class?