java - How do I use Realm with generic type? -
i have generic method , retrieve objects using generic type. method:
public static <t extends realmobject & indentifiermodel> void storenewdata() { ... t item = realm.where(class<t>) // not compiling (expression not expected) .equalto("id", data.get(i).getid()) .findfirst(); } the above isn't working realm.where(class<t>). how pass in generic type realm?
you have supply generic parameter so:
public static <t extends realmobject & indentifiermodel> void storenewdata(class<t> clazz) { t item = realm.where(clazz) .equalto("id", 123) .findfirst(); } class<t> not valid, since that's saying realm.where(class<list<string>>) or realm.where(class<string>). need actual class<t> instance. cannot use t.class either since t not available @ runtime due type-erasure. @ runtime, method needs class<t> instance work properly. since cannot t, have explicitly supply argument of type class<t>.
Comments
Post a Comment