2017-04-17 53 views
1

我有一個模型Android的 - 保存realmlist一種境界對象

public class Response extends RealmObject { 

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

現在裏面,我試圖挽救task_responses realmlist響應內,但未能成功,下面是相關代碼:

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

當我放置調試這一行

response.setTaskResponses(taskResponses); 

taskResponses的大小爲5,但前後執行它變成空的,realm存儲一個空列表。

TaskResponse型號:

public class TaskResponse extends RealmObject { 

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

回答

1

您可以保存這樣它會工作

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(); 
+0

taskResponses沒有得到保存,其餘的排序 –

+0

檢查我現在已經編輯的代碼,將工作 –

+0

我有聲明mRealm已經但無法將realmList taskResponses存儲到響應中 –