Realm-java: Generated RealmObjectProxy has wrong lookup for @Required String primary key with 0.89.0

Created on 21 Apr 2016  路  4Comments  路  Source: realm/realm-java

Steps & Code to Reproduce

Create a RealmObject with a String field marked with @PrimaryKey and @Required.

public class RealmUser extends RealmObject {
    @PrimaryKey
    @Required
    public String _id;
}

The generated RealmObjectProxy class contains an invalid lookup for the primary key in the copyOrUpdate method. The compile-time exception is:

RealmUserRealmProxy.java:276: error: incompatible types: String cannot be converted to long
            long rowIndex = table.findFirstLong(pkColumnIndex, ((RealmUserRealmProxyInterface) object).realmGet$_id());
                                                                                                                   ^

I traced what I think is the wrong codepath here: https://github.com/realm/realm-java/blob/master/realm/realm-annotations-processor/src/main/java/io/realm/processor/RealmProxyClassGenerator.java#L642
The relevant generated code is:

public static RealmUser copyOrUpdate(Realm realm, RealmUser object, boolean update, Map<RealmModel,RealmObjectProxy> cache) {
    if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm().threadId != realm.threadId) {
        throw new IllegalArgumentException("Objects which belong to Realm instances in other threads cannot be copied into this Realm instance.");
    }
    if (object instanceof RealmObjectProxy && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath())) {
        return object;
    }
    RealmUser realmObject = null;
    boolean canUpdate = update;
    if (canUpdate) {
        Table table = realm.getTable(RealmUser.class);
        long pkColumnIndex = table.getPrimaryKey();
        long rowIndex = table.findFirstLong(pkColumnIndex, ((RealmUserRealmProxyInterface) object).realmGet$_id());
        if (rowIndex != TableOrView.NO_MATCH) {
            realmObject = new RealmUserRealmProxy(realm.schema.getColumnInfo(RealmUser.class));
            ((RealmObjectProxy)realmObject).realmGet$proxyState().setRealm$realm(realm);
            ((RealmObjectProxy)realmObject).realmGet$proxyState().setRow$realm(table.getUncheckedRow(rowIndex));
            cache.put(object, (RealmObjectProxy) realmObject);
        } else {
            canUpdate = false;
        }
    }

    if (canUpdate) {
        return update(realm, realmObject, object, cache);
    } else {
        return copy(realm, object, update, cache);
    }
}

Version of Realm and tooling

Realm version(s): 0.89.0

Android Studio version: 2.1 Beta 3

Which Android version and device: N/A

T-Bug

Most helpful comment

Hi @dmarcato

The issue is addressed in #2653 and new version 0.89.1 containing the fix is just released.
Hope it solves the error. I'm closing the issue now.

All 4 comments

Hi @dmarcato

Thank you for the contribution. The issue is seen very clearly, and it will be fixed soon.

Wait a second, does this mean that String IDs just _don't work at all_ with 0.89.0?

Hi @Zhuinden,

This issue only happens when you are to enforce a String type primary key field be _not null_ by further marking the field as @Required. We will release a hotfix very soon.

Hi @dmarcato

The issue is addressed in #2653 and new version 0.89.1 containing the fix is just released.
Hope it solves the error. I'm closing the issue now.

Was this page helpful?
0 / 5 - 0 ratings