0
我有这个类如何在关系中删除领域?
class Student extends RealmObject {
public String code;
public String name;
public String email;
public Course course;
}
class Course extends RealmObject {
public String code;
public String name;
}
class Sync {
// ...
// To sync data I am using retrofit, look the method to update course
public void onResponse(Call<...> call, Response<...> response) {
if (response.isSuccessful()) {
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.delete(Course.class);
realm.copyToRealm(response.body());
}
});
}
}
}
后调用同步更新课程,所有学生对象都有其课程设置为null,这叫做境界后,预期的行为删除? 即使再次填充表后,Student上的课程仍为空。
今天我做的代码这一变化:
class Course extends RealmObject {
@PrimaryKey
public String code;
public String name;
}
class Sync {
// ...
// To sync data I am using retrofit, look the method to update course
public void onResponse(Call<...> call, Response<...> response) {
if (response.isSuccessful()) {
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(response.body());
}
});
}
}
}
我做了这个为时已晚,以避免删除课程。
我能做些什么来恢复参考课程并将其重新设置为学生?
谢谢。
谢谢。我认为即使在删除课程之后,Realm也会在学生中保留某种身份证件。表格再次填充后,可以恢复引用。我遵循你的提示使用注解'@ PrimaryKey',现在我正在避免从领域删除对象。我会解决这个问题。 – diegofesanto