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