2

我有一个模型

public class Response extends RealmObject {

        @PrimaryKey
        private String _id;
        private RealmList<TaskResponse> task_responses;
        private AssignmentRecord assignmentRecord;
        private ResponseStatistic responseStats;
}

现在,我正在尝试将 task_responses realmlist 保存在 Response 中,但未能成功,以下是相关代码:

    mRealm.beginTransaction();
    response.setTaskResponses(taskResponses);
    response.setAssignmentRecord(assignmentRecord);
    response.setResponseStats(responseStatistic);
    mRealm.commitTransaction();

当我在此行之前放置调试器时

response.setTaskResponses(taskResponses);

taskResponses 的大小为 5,但执行后它变为空并且领域存储一个空列表。

任务响应模型:

    public class TaskResponse extends RealmObject {

        @PrimaryKey
        private String _id;
        private String taskId;
        private Boolean answered;
        private String answer;
        private TaskStatistic taskStat;
    }
4

1 回答 1

2

您可以像这样保存它会起作用

Realm mRealm = Realm.getDefaultInstance();
    mRealm.beginTransaction();
    RealmList<TaskResponse> taskResponseslist = new RealmList();
    taskResponseslist.addAll(taskResponses);
    response.setTaskResponses(taskResponseslist);
    response.setAssignmentRecord(assignmentRecord);
    response.setResponseStats(responseStatistic);
    mRealm.commitTransaction();
    mRealm.close();
于 2017-04-17T11:30:51.943 回答