Passing RealmObect as paramters in functions

46 views
Skip to first unread message

Johnny TwoShoes

unread,
Dec 31, 2015, 12:19:36 PM12/31/15
to Realm Java
Hi,

if I have class X extends RealmObject{
}

and class Y extends RealmObject {
}

how do I have a generic function that can proces both types  eg

public void funcgen(Object obj,string s){

realm.createOrUpdateAllFromJson(obj, obj.getJSONArray(s));
//realm.createOrUpdateAllFromJson(Y.class, obj.getJSONArray("yy"));
//realm.createOrUpdateAllFromJson(X.class, obj.getJSONArray("xx"));
}

regards

Mulong Chen

unread,
Jan 3, 2016, 8:33:56 PM1/3/16
to Johnny Twoshoes, realm...@googlegroups.com
Hi,

How does your JSON string look like?

--
Mulong Chen

{#HS:155988876-2580#}
--
You received this message because you are subscribed to the Google Groups "Realm Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-java+...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-java/395a4f22-9424-45be-bafa-74447c1615ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Johnny TwoShoes

unread,
Jan 4, 2016, 3:54:05 AM1/4/16
to Mulong Chen, realm...@googlegroups.com
Hi,

If XX and YY are totally different, then is it possible to have a generic function that can happen both classes?
Sorry, if this is a basic question?
Or should I use a string, to generate the class? 

regards

Christian Melchior

unread,
Jan 4, 2016, 4:36:03 AM1/4/16
to Johnny Twoshoes, realm...@googlegroups.com
Hi Johnny

You can do

public <E extends RealmObject> void funcgen(Class<E> clazz, Object obj, string s) {
realm.createOrUpdateAllFromJson(clazz , obj.getJSONArray(s));
}


--
Christian Melchior
Senior Android Developer
{#HS:155988876-2580#}
On Mon, Jan 4, 2016 at 8:54 AM UTC, Johnny Twoshoes <johnnyt...@gmail.com> wrote:
Hi,

If XX and YY are totally different, then is it possible to have a generic function that can happen both classes?
Sorry, if this is a basic question?
Or should I use a string, to generate the class?

regards



On Mon, Jan 4, 2016 at 1:33 AM UTC, Mulong Chen <he...@realm.io> wrote:
Hi,

How does your JSON string look like?

--
Mulong Chen



Johnny TwoShoes

unread,
Jan 5, 2016, 6:24:14 PM1/5/16
to Christian Melchior, realm...@googlegroups.com
OK Thanks alot. Can I ask more one question please, since, I looked at the docs, but, couldn't find the answer.
if object x, contains a list of object y, and I want to select a Y object, can I do that with a query?
X x = realm.where(X.class).equalTo("id", idx).findFirst();
Y y = x.where(Y.class).equalTo("id", idy).findFirst();
???
regards
Reply all
Reply to author
Forward
0 new messages