Mathijs Segers
Mathijs Segers

Reputation: 6238

Using Reflection with Realm (Android)

I made a switch to Realm last week. Now, I've got this issue when using reflection:

Normally I'd use reflection to fetch field, which when available I'd use to replace url params in our API url f/e: blog/{blogId}/comments <- not an actual url but an example.

The code would check if there is a field called blogId, if yes it'll invoke the getter (getBlogId) and replace the value in the path. Now with realm I get a "BlogRealmProxy" which does not contain the properties I was expecting on my RealmObject, it has fields like INDEX_BLOGID (or alike).

Any idea how to use reflection still? I need this to have generic functions available.

Update:

Well I found a way, by doing something quite silly, any better options?:

Class clazz = obj.getClass();

    if (clazz.getName().endsWith("Proxy")) {
        clazz = clazz.getSuperclass();
    }

I'm 100% sure objects will never end with Proxy ;)

Upvotes: 2

Views: 609

Answers (1)

Mathijs Segers
Mathijs Segers

Reputation: 6238

Well I found a way, by doing something quite silly, any better options?:

Class clazz = obj.getClass();

if (clazz.getName().endsWith("Proxy")) {
    clazz = clazz.getSuperclass();
}

I'm 100% sure objects will never end with Proxy ;) in my app.

Upvotes: 3

Related Questions