我有一个模型,比如PostModel
和另一个模型,它存储PostModel创建时的id和日期。
public class RealmPostModel extends RealmObject {
@PrimaryKey
private int id;
private Date date;
private PostModel postModel;
}
public class PostModel extends RealmObject{
@PrimaryKey
public int post_id;
}
我要保存的内容(我在onPause
中执行此操作)是:
MIApplication.realm.beginTransaction();
RealmPostModel singlePostModelRealmResults = MIApplication.realm.where(RealmPostModel.class)
.equalTo("id", Integer.valueOf(clickedPostId)).findFirst();
if (singlePostModelRealmResults != null){
singlePostModelRealmResults.deleteFromRealm();
}
RealmPostModel realmSinglePostModel = new RealmPostModel(Integer.valueOf(clickedPostId),
new Date(),
new PostModel(singlePostAdapter.getmDataset()));
// Here, realmSinglePostModel shows that it has PostModel
// and not null
MIApplication.realm.copyToRealmOrUpdate(realmSinglePostModel);
MIApplication.realm.commitTransaction();
要访问它(我在onViewCreated
中执行此操作)是:
MIApplication.realm.beginTransaction();
RealmPostModel singlePostModelRealmResults = MIApplication.realm.where(RealmPostModel.class)
.equalTo("id", Integer.valueOf(clickedPostId)).findFirst();
MIApplication.realm.commitTransaction();
// Here singlePostModelRealmResults shows that PostModel is null inside
// RealmPostModel where id is clickedPostId
问题是,PostModel
对象里面的RealmPostModel
对象在我检索它时总是为空。我实际上在做Realm
错了吗?
编辑:
在这里,
postModel:PostModel
。但是当我ctrl+f1
检查细节时,它说PostModel为null。但正如文档中所述,这是一种误报。
我实际上只能使用RealmPostModel
id作为PostModel
的外键,因为它们是相同的。为方便起见,我做了我做的事情(RealmPostModel
有3列,一列指向PostModel
)。