Android - Realm没有在模型中获取对象>编辑未在领域对象中获取Realm对象

时间:2016-09-06 07:23:20

标签: android realm

我有一个模型,比如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 added 在这里,postModel:PostModel。但是当我ctrl+f1检查细节时,它说PostModel为null。但正如文档中所述,这是一种误报。

Here PostModel is null 当我从表中检索时,PostModel为空。

我实际上只能使用RealmPostModel id作为PostModel的外键,因为它们是相同的。为方便起见,我做了我做的事情(RealmPostModel有3列,一列指向PostModel)。

0 个答案:

没有答案