Realm.io can't get examples to work

a 夏天 提交于 2019-12-12 12:19:40

问题


I'm failing to get started with Realm.io, I've tried it in my own project aswell as with the IntroExample.

When i try to look up something, i get:

java.lang.IllegalStateException: Mutable method call during read transaction.

When i try to store something, i get:

io.realm.exceptions.RealmException: Could not find the generated proxy class

I seem to have a fundamental flaw somewhere.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "lorem.ipsum"
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'org.jsoup:jsoup:1.8.+'
    compile 'io.realm:realm-android:0.71.0'
}

回答1:


Make a @RealmClass annotation to your model class. I struggled with the same problem and went a bit deeper in the proxy class generation of Realm. Only classes with the @RealmClass annotation are generated.

@RealmClass
public class Category extends RealmObject {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}



回答2:


I tried to run your example code and got a lot of exceptions regarding your model class. It looks like your getters and setters have been renamed from the default, which currently breaks the annotation processor.

I tried to change the model class to:

public class DataItem extends RealmObject {
    private String uuid;
    private String content;
    private boolean bool;

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public boolean isBool() {
        return bool;
    }

    public void setBool(boolean bool) {
        this.bool = bool;
    }
}

which seemed to work.




回答3:


A combination of answer https://stackoverflow.com/a/26294208/1251958 and answer https://stackoverflow.com/a/26326314/1251958 leads to success.

After adding the @RealmClass annotation @Philluxx metioned, i got errors in AndroidStudio pointing out the required syntax as mentioned by @ChristianMelchior

Note: You will probably have to clear the app data as neglecting to do either seems to lead to a corrupted database that leads to subsequent crashes.




回答4:


Emanuele from Realm here.

I took a look at the example project you posted in the comments and the only problem I found was in the naming of the getters and setters. We surely need to document this better but to easily get a predictable behavior you should let the IDE autogenerate the getters and setters for you. One note about booleans: it looks that if you have a boolean callaed isValid Android Studio will generate accessors named isValid() and setValid(). I'm not sure how Eclipse behaves in the same scenario. Anyway, in the annotation processor we generate something like isIsValid() and setIsValid() which is of course ugly and will be fixed soon. Anyway, if you name your boolean simply valid everything will work fine.



来源:https://stackoverflow.com/questions/26272382/realm-io-cant-get-examples-to-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!