Theo Theo - 3 months ago 23
Android Question

Objects not stored in my Realm database

I am making an excersice to understand how Realm is working. The example is having two model classes. One is called User and the other one is called Task. So each user will have a corresponing task.

Here are the two model classes.

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;
}
}


and

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;
}
}


Now in my main fragment I am trying to store those objects in my database.

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();
}
}


The response I get is:

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


I am using the 1.2.0 Realm version.

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
}


Any ideas what could be wrong?

Thanks,

Theo.

Answer

In order for the annotation processor to work on your Realm model classes, you have two options:

1.)

public class Thing extends RealmObject {

2.)

@RealmClass
public class Thing implements RealmModel {

If you look at the source for RealmObject, it starts like this too:

@RealmClass
public abstract class RealmObject implements RealmModel {