我非常了解Realm是如何运作的。该示例具有两个模型类。一个叫做User,另一个叫做Task。因此每个用户都有相应的任务。
以下是两个模型类。
public class User extends RealmObject {
@PrimaryKey
private String id;
private String firstName;
private String lastName;
private Task task;
private RealmList<Task> tasks;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Task getTask() {
return task;
}
public void setTask(Task task) {
this.task = task;
}
public RealmList<Task> getTasks() {
return tasks;
}
public void setTasks(RealmList<Task> tasks) {
this.tasks = tasks;
}
}
和
public class Task implements RealmModel, Comparable<Task> {
@PrimaryKey
private String id;
private String title;
private String description;
private boolean isCompleted;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public int compareTo(Task another) {
return 0;
}
public boolean isCompleted() {
return isCompleted;
}
public void setCompleted(boolean completed) {
isCompleted = completed;
}
}
现在在我的主片段中,我试图将这些对象存储在我的数据库中。
public class MainFragment extends Fragment {
private static final String TAG = MainFragment.class.getSimpleName();
private Realm realm;
public MainFragment() {
}
public static MainFragment newInstance() {
Bundle args = new Bundle();
MainFragment fragment = new MainFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
realm = Realm.getDefaultInstance();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
// Only create a user if we don't have one.
if (realm.where(User.class).count() == 0) {
User u = realm.createObject(User.class);
u.setFirstName("Theo");
u.setLastName("Tziomakas");
u.setId(UUID.randomUUID().toString());
}
}
});
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
User u = realm.where(User.class).findFirst();
Task t = realm.createObject(Task.class);
t.setTitle("Test Task");
t.setDescription("Foo Bar");
u.getTasks().add(t);
}
});
User u = realm.where(User.class).findFirst();
Log.d(TAG, u.getTask().getTitle());
}
@Override
public void onDestroy() {
super.onDestroy();
realm.close();
}
}
我得到的回应是:
C:\Users\Theo\AndroidStudioProjects\ReamTutorial2\app\build\generated\source\apt\debug\io\realm\UserRealmProxy.java
Error:(349, 66) error: cannot find symbol variable TaskRealmProxy
Error:(183, 17) error: cannot find symbol variable TaskRealmProxy
Error:(338, 65) error: cannot find symbol variable TaskRealmProxy
Error:(187, 17) error: cannot find symbol variable TaskRealmProxy
Error:(471, 75) error: cannot find symbol variable TaskRealmProxy
Error:(400, 70) error: cannot find symbol variable TaskRealmProxy
Error:(390, 69) error: cannot find symbol variable TaskRealmProxy
我正在使用1.2.0 Realm版本。
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath "io.realm:realm-gradle-plugin:1.2.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
任何想法可能出错?
谢谢,
西奥。
答案 0 :(得分:1)
为了使注释处理器能够处理您的Realm模型类,您有两个选择:
1。)
public class Thing extends RealmObject {
2)。
@RealmClass
public class Thing implements RealmModel {
如果您查看RealmObject
的来源,它也会这样开始:
@RealmClass
public abstract class RealmObject implements RealmModel {