How can i serialize a Realm object which has a custom class

459 Views Asked by At

I'm having a problem with building Realm object which has a custom class.

@Parcel(
    value = Parcel.Serialization.BEAN,
    analyze = { Message.class })

@RealmClass
public class Message implements Comparable<Message>, RealmModel {`

@PrimaryKey
@Index
private long id;
@JsonProperty("thread_id")
long threadId;
@JsonProperty("message")
public String message;
@JsonProperty("user")
public User user;
...
}

When server send json response, try to parse as Message realm object with

realm.createObjectFromJson(MesssageMessage.class, JSONObject)

The problem is User. I've got an compile error "Filed user is not supported".

Below is the User class which is not realm object.

@JsonIgnoreProperties("incomplete_signed_up")
public class User implements KeepClassFromProguard, Parcelable {
public static final Parcelable.Creator<User> CREATOR = new
Parcelable.Creator<User>() {


    @Override
    public User createFromParcel(Parcel in) {
        return new User(in);
    }

    @Override
    public User[] newArray(int size) {
        return new User[size];
    }
};

public long id;
@JsonProperty("account_id")
private long accountId;
@JsonProperty("display_name")
public String display_name;
@JsonProperty("nick_name")
public String nickname;
@JsonProperty("user_detail")
public UserDetail userDetail;

...
}

I read https://gist.github.com/cmelchior/ddac8efd018123a1e53a and http://parceler.org/#getting_parceler, but I could't get answer yet. I can't change every class to realm object because they all have another custom classes.

Does anyone know how this is handled? Hope there is any good example. Thanks.

1

There are 1 best solutions below

0
On

All references in a RealmModel must reference other RealmModel classes. This is the only way we can persist them.

You can @Ignore the user field, but then it will not be saved by Realm.