Realm-java: Conflict with Moshi

Created on 2 May 2016  路  13Comments  路  Source: realm/realm-java

Goal

We have a conflict between RealmChangeListener and something of Moshi.
Please check this bug here: https://github.com/square/moshi/issues/159

Actual Results

Runtime exception, like this:

java.lang.IllegalArgumentException: No JsonAdapter for interface io.realm.RealmChangeListener annotated []

Steps & Code to Reproduce

Please check the last link.

Code Sample

Please check the last link.

Version of Realm and tooling

T-Help

Most helpful comment

Thanks for all the feedback, I have created a ready to go RealmListAdapter for Moshi in this library:
https://github.com/Commit451/Regalia

You can see the source here if you want to pull in the class manually:
https://github.com/Commit451/Regalia/tree/master/regalia-moshi/src/main/java/com/commit451/regalia/moshi

All 13 comments

Hi @ppamorim
From 0.89.0 there shouldn't be any fields in RealmObject anymore and we never had a listener field, so it sounds a bit strange. Maybe Moshi is somehow looking into the types on methods we have in RealmObject. I'll look into into.

In the meantime, can you try switching from extends RealmObject to implements RealmModel instead? That is our new interface we introduced in 0.89 and it doesn't have any state/methods on it. It could be interesting to see if it makes a difference.

Hi @ppamorim
I tried creating a new project where this was the _only_ model class:

open class Foo(open var id : Int, open var name : String) : RealmObject(), Parcelable {

    companion object {
        @JvmField final val CREATOR: Parcelable.Creator<Foo> =
                object : Parcelable.Creator<Foo> {
                    override fun createFromParcel(source: Parcel): Foo= Foo(source)
                    override fun newArray(size: Int): Array<Foo?> = arrayOfNulls(size)
                }
    }

    constructor() : this(0, "")

    constructor(parcel: Parcel) : this(parcel.readInt(), parcel.readString())

    override fun writeToParcel(parcel: Parcel, flag: Int) {
        parcel.writeInt(id)
        parcel.writeString(name)
    }

    override fun describeContents(): Int = 0

}

That seemed to compile fine, but if I added a model class that referenced other model classes I got an error message like this: Caused by: java.lang.IllegalArgumentException: Platform java.lang.Class<io.realm.examples.kotlin.model.Cat> annotated [] requires explicit JsonAdapter to be registered

That doesn't seem to match what you are seeing though. Can you create a small sample project that displays the behaviour you are seeing?

@cmelchior I tested with @RealmClass and RealmModel and works perfectly! Thanks for this.

Btw, have you any problem with relationship of models?

Not that I am aware of? Are you seeing any problems when using them?

I'll review. Wait a little.

@ppamorim Any updates for this issue? Shall I close it?

@ppamorim We haven't head from you in two weeks so we're gonna close this issue. Please contact us if you need it to be reopened.

@cmelchior I encountered the same issue you pointed out when referencing other models, regardless whether they were extending RealmObject or implementing RealmModel:
Caused by: java.lang.IllegalArgumentException: Platform java.lang.Class<xyz.projectplay.moshirealm.Baz> annotated [] requires explicit JsonAdapter to be registered

It seems to only happen when trying to use a RealmList<>.

Have any insight to this?

@Reline Based on moshi source code, you need to create a CollectionJsonAdapter for RealmList https://github.com/square/moshi/blob/master/moshi/src/main/java/com/squareup/moshi/CollectionJsonAdapter.java#L34

@Zhuinden thanks, I was able to parse it successfully with your suggestion

public class RealmListJsonAdapter {

    @FromJson
    public RealmList<Baz> fromJson(Collection<Baz> collection) {
        RealmList<Baz> realmList = new RealmList<>();
        realmList.addAll(collection);
        return realmList;
    }

    @ToJson
    public Collection<Baz> toJson(RealmList<Baz> realmList) {
        return realmList;
    }
}

but then realized that I would have to write this everywhere I use a RealmList<> after trying to create an adapter using generics: requires explicit JsonAdapter

Thanks for all the feedback, I have created a ready to go RealmListAdapter for Moshi in this library:
https://github.com/Commit451/Regalia

You can see the source here if you want to pull in the class manually:
https://github.com/Commit451/Regalia/tree/master/regalia-moshi/src/main/java/com/commit451/regalia/moshi

@Jawnnypoo How exactly do I use your class? I've added it to my project. How do I modify my RealmModel to make use of this class?

public class MenuModel implements RealmModel { @Json(name = "menugroups") private RealmList<MenuGroupModel> menugroups = null; }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tloshi picture tloshi  路  3Comments

wezley98 picture wezley98  路  3Comments

aschrijver picture aschrijver  路  3Comments

AlbertVilaCalvo picture AlbertVilaCalvo  路  3Comments

Merlin1993 picture Merlin1993  路  3Comments