Hello its my first time to use realm and i think its great it saves me time creating SQLite database for my app, however i tried passing RealmObject that implements Serializable from one Activity to another and it doesn't work. it throws NullPointerException, I'm using version realm-81.1.jar
please see my sample code below.
==== Sending Part ====
Intent intent = new Intent(sending class, receiving class);
intent.putExtra("entity", testmodel);
startActivity(intent)
==== Receiving Part ====
TestModel model = (TestModel) getIntent().getSerializableExtra("entity");
==== TestModel Code ====
public class TestModel extends RealmObject implements Serializable {
private String name;
private int id;
// getter and setter
}
I would suggest you put "id" field into extra and inside receiving part do request into database. Otherwise you probably have to manual deep copy your property.
hi @mshusek thanks for the reply i was beginning to think i'm alone in here hehe anyways, apparently i ended up doing that passing the id to the receiving part and query from there its not a bad idea but it would nicer if we could pass the RealmObject in an Intent as Serializable so we only have to query it once.
Right now Realm is not very good for implementing Serializable. We are working on a solution that will make RealmObjects feel a lot more like normal POJOs, but until then some workarounds are needed.
For example, why serialize when your data is in Realm already? As suggested you can add a primary key field and only pass that around and requery your data/object on the other side.
Hi @emanuelez once again thanks for giving a light on my post. i totally get your point. in response to your question "why serialize when your data is in Realm already?" its because why requery if i already queried that data on realm on another page and ill be using that same data on the other page. passing it as a Serializable would be much easier.
I really appreciate your response, seems everybody is really busy making this much better.
ill be closing this post now
Well, re-querying is actually a much lighter operation, especially when operated on a primary key :)
is that so.. in that case i'm gonna take your advice.
so my question is... will RealmObjects ever be able to implement Serializable? is it in the plans?
RealmList doesn't implement it and most likely won't.
No, we don't have any plans to support Serializable.
Most helpful comment
No, we don't have any plans to support
Serializable.