改造2与Realm的冲突

时间:2018-05-21 15:12:13

标签: android realm retrofit retrofit2

尝试通过Retrofit 2发送请求后,我收到了Background partial concurrent mark sweep GC freed...。可能是因为RealmObject数组我用来发送请求@Body

我的API界面:

@POST("sessions/add")
Call<ResponseBody> sessionsAdd(@Body SessionVO[] options, @Header("Authorization") String token);

请求发送代码:

SessionVO[] sessionVOArray= sessionsRealm.getAll();
call = serverApi.sessionsAdd(sessionVOArray, getSharedPreferenceManager().getAccessToken());

SessionVO课程:

public class SessionVO extends RealmObject {

    private long sessionStartTime;
    private long sessionEndTime;

    public long getSessionStartTime() {
        return sessionStartTime;
    }

    public void setSessionStartTime(long sessionStartTime) {
        this.sessionStartTime = sessionStartTime;
    }

    public long getSessionEndTime() {
        return sessionEndTime;
    }

    public void setSessionEndTime(long sessionEndTime) {
        this.sessionEndTime = sessionEndTime;
    }
}

领域部分:

public class SessionsRealm extends RealmObject {

    private SessionVO sessionVO;

    @Ignore
    private Realm realm;

    public SessionVO getSessionVO() {
        return sessionVO;
    }

    public void setSessionVO(SessionVO sessionVO) {
        this.sessionVO = sessionVO;
    }

    public SessionsRealm(){
        realm= Realm.getDefaultInstance();
    }

    private void insert(final SessionsRealm sessionsRealm){
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                realm.insert(sessionsRealm);
            }
        });
    }

    public SessionVO[] getAll(){
        return Realm.getDefaultInstance().where(SessionVO.class).findAll().toArray(new SessionVO[Realm.getDefaultInstance().where(SessionsRealm.class).findAll().size()]);
    }

    public boolean isEmpty(){
        return Realm.getDefaultInstance().isEmpty();
    }

    public void deleteAll(){
        Realm realm= Realm.getDefaultInstance();
        if(!realm.isEmpty()){
            realm.beginTransaction();
            realm.deleteAll();
            realm.commitTransaction();
        }
        realm.close();
    }

    public void save(Object vo){
        SessionsRealm sessionsRealm= new SessionsRealm();
        sessionsRealm.setSessionVO((SessionVO) vo);
        sessionsRealm.insert(sessionsRealm);
    }

    public void closeRealm(){
        realm.close();
    }

}

改造初始化部分:

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .build();

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        Retrofit.Builder builder = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create(gson));

Retrofit retrofit = builder.build();

1 个答案:

答案 0 :(得分:1)

return Realm.getDefaultInstance().where(SessionVO.class).findAll().toArray(new SessionVO[Realm.getDefaultInstance().where(SessionsRealm.class).findAll().size()]);

此方法打开两个永远不会关闭的Realm实例,但除此之外,大部分实际上都是不必要的。您可以将RealmResults用作列表,因此您甚至不需要数组。

public RealmResults<SessionVO> getAll(Realm realm){
    return realm.where(SessionVO.class).findAll();
}

虽然要通过GSON的Retrofit发送,但在传递给@Body之前可能需要调用copyFromRealm