Realm.insertOrUpdate和copyToOrUpdate不起作用,但createObject确实有效

时间:2017-07-05 13:01:34

标签: java android realm

我有一个事件列表,我试图像这样保存:

我的活动课程:

public class Event extends RealmObject {

@PrimaryKey
private long id;

private String title;
private Date eventStart;
//some more ints and strings
}

插入/复制

realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        // This doesn't work and exits the method entirely
        realm.insertOrUpdate(events);

        // This doesn't work and exits the method entirely
        realm.copyToRealmOrUpdate(events.get(0));

        // This workaround works but only if there are no duplicates, or primary keys
        for (Event event: events) {
            Event realmEvent = realm.createObject(Event.class, event.getId());
            realmEvent.setTitle(event.getTitle());
            realmEvent.setEventState(event.getEventStart());
        }
    }
});

有些课程,copyToRealm可以使用,而其他课程则不适用。

我在调试模式中逐步完成代码并注意到在生成的DefaultRealmModuleMediator.java中,它似乎没有识别出我将它作为正确的RealmObject提供的RealmObject。它涉及到这一部分:

@Override
public void insertOrUpdate(Realm realm, Collection<? extends RealmModel> objects) {
Iterator<? extends RealmModel> iterator = objects.iterator();
    RealmModel object = null;
    Map<RealmModel, Long> cache = new HashMap<RealmModel, Long>(objects.size());
    if (iterator.hasNext()) {
    @SuppressWarnings("unchecked") Class<RealmModel> clazz = (Class<RealmModel>) ((object instanceof RealmObjectProxy) ? object.getClass().getSuperclass() : object.getClass());

    if (clazz.equals(com.example.models.Event.class)) {
        io.realm.EventRealmProxy.insertOrUpdate(realm, (com.example.models.Event) object, cache);
    } else {
        throw getMissingProxyClassException(clazz);
    }

然后它的计算结果为false并抛出该异常,就像传递给它的Event.class实际上不是Event.class对象一样。 但调试器将clazz对象列为com.example.models.Event。

异常是&#34; RealmObject不是此Realm&#34;

的架构的一部分

我的项目级别gradle文件:

dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3'
    classpath 'com.google.gms:google-services:3.0.0'
    classpath "io.realm:realm-gradle-plugin:3.4.0"
}

我的应用级Gradle文件:

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }

    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.22.1'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'realm-android'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

android {
... //rest of dependencies etc.

apply plugin: 'com.google.gms.google-services'

知道这里可能会发生什么吗?

0 个答案:

没有答案