Java 域不保存数据

Java 域不保存数据,java,android,realm,Java,Android,Realm,我正在尝试保存从API获取的数据,我的函数如下所示 试一试{ //模拟网络访问。 mNetworkSubscription=NetworkRequest.performAsyncRequest(api.getPatientsData(tenantID),(数据)->{ //更新主线程上的UI 试一试{ //长pat_id=0; 如果(数据!=null){ if(data.getAsJsonObject().get(“错误”)!=null){ publishResults(“getPatients

我正在尝试保存从API获取的数据,我的函数如下所示 试一试{ //模拟网络访问。 mNetworkSubscription=NetworkRequest.performAsyncRequest(api.getPatientsData(tenantID),(数据)->{ //更新主线程上的UI 试一试{ //长pat_id=0; 如果(数据!=null){ if(data.getAsJsonObject().get(“错误”)!=null){ publishResults(“getPatientsData”,状态错误,空); }

                    if (data != null && data.get("result") != null && data.get("result").toString() != "false") {

                        Realm realm = Realm.getDefaultInstance();

                        JsonParser parser = new JsonParser();
                       // realm.beginTransaction();


                       // realm.where(Patient.class).findAll().deleteAllFromRealm();

                        //realm.commitTransaction();

                        try {

                            JsonArray casesJsonArray =  data.get("result").getAsJsonArray();//parser.parse(data.get("result").getAsJsonObject().toString()).getAsJsonArray();
                            Log.v(Constants.TAG,"PatientJsonArray: "+casesJsonArray);
                            if(casesJsonArray.size() > 0) {
                                Patient patientRecord = new Patient();

                                realm.executeTransaction(new Realm.Transaction() {
                                    @Override
                                    public void execute(Realm realm) {
                                        for (int i = 0; i < casesJsonArray.size(); i++) {
                                            try {

                                                JsonObject jsonObject = (JsonObject) casesJsonArray.get(i);
                                                Log.e(Constants.TAG, "execute: jsonObject "+jsonObject);
                                                Log.e(Constants.TAG, "execute: id "+id);

                                                if(realm.where(Patient.class).max("id") != null) {
                                                    id = realm.where(Patient.class).max("id").intValue();
                                                    Log.e(Constants.TAG, "execute:getPatientsData : In DataSync:  " + id);
                                                }

                                                Log.e(Constants.TAG, "execute: jsonObject "+jsonObject);
                                                Log.e(Constants.TAG, "execute: id "+pat_id);


                                                patientRecord.setId(id + 1);
                                                patientRecord.setTenantID(jsonObject.get("tenant_id").getAsLong());
                                                patientRecord.setPatientID(jsonObject.get("patient_id").getAsLong());
                                                patientRecord.setFirstName(jsonObject.get("first_name").getAsString());
                                                patientRecord.setLastName(jsonObject.get("last_name").getAsString());
                                                patientRecord.setPatientWeight(jsonObject.get("patient_weight").getAsString());
                                                patientRecord.setPatientAge(jsonObject.get("patient_age").getAsString());
                                                patientRecord.setGender(jsonObject.get("gender").getAsString());
                                                patientRecord.setStreetAddress(jsonObject.get("street_address").getAsString());
                                                patientRecord.setArea(jsonObject.get("area").getAsString());
                                                patientRecord.setCity(jsonObject.get("city").getAsString());
                                                patientRecord.setZipcode(jsonObject.get("zipcode").getAsString());
                                                patientRecord.setState(jsonObject.get("state").getAsString());
                                                patientRecord.setEmail(jsonObject.get("email").getAsString());
                                                patientRecord.setEnqNumber(jsonObject.get("alternate_number").getAsString());
                                                patientRecord.setEnqName(jsonObject.get("enquirer_name").getAsString());

                                                realm.copyToRealmOrUpdate(patientRecord);
                                            }catch (Exception e){
                                                Log.v(Constants.TAG,"caseList Insert exception: "+e.toString());
                                            }
                                        }
                                    }

                                });
                                //realm.close();
                            }

                        } catch (Exception e) {
                            Log.v(Constants.TAG, "getPatientsData Exception: " + e.toString());
                        }

                        realm.close();
                    }
                }
            } catch (Exception e) {
                Log.e(Constants.TAG, "getPatientsData() exception: " + e.toString());
                publishResults("getPatientsData",STATUS_ERROR, null);
            }finally {
                publishResults("getPatientsData",STATUS_FINISHED, null);
            }
        }, (error) -> {
            // Handle Error
            Log.e(Constants.TAG,"getPatientsData() Error: "+error.getCause());
            publishResults("getPatientsData",STATUS_ERROR, null);
        });

    }catch (Exception e){
        Log.e(Constants.TAG,"getPatientsData() Exception: "+e.toString());
        publishResults("getPatientsData",STATUS_ERROR, null);
    }
if(data!=null&&data.get(“结果”)!=null&&data.get(“结果”).toString()!=“false”){
Realm Realm=Realm.getDefaultInstance();
JsonParser=新的JsonParser();
//realm.beginTransaction();
//realm.where(Patient.class).findAll().deleteAllFromRealm();
//realm.commitTransaction();
试一试{
JsonArray casesjssonarray=data.get(“结果”).getAsJsonArray();//parser.parse(data.get(“结果”).getAsJsonObject().toString()).getAsJsonArray();
Log.v(Constants.TAG,“PatientJsonArray:+casesJsonArray”);
如果(casesjsharray.size()>0){
患者记录=新患者();
realm.executeTransaction(新realm.Transaction(){
@凌驾
公共void执行(领域){
对于(int i=0;i{
//处理错误
Log.e(Constants.TAG,“getPatientsData()错误:”+Error.getCause());
publishResults(“getPatientsData”,状态错误,空);
});
}捕获(例外e){
Log.e(Constants.TAG,“getPatientsData()异常:”+e.toString());
publishResults(“getPatientsData”,状态错误,空);
}
但是realm.copyrupdate(patientRecord
Number num = realm.where(Patient.class).max("id");
int l_id;
if (num == null) {
  i_id = 0;
} else {
  i_id = num.intValue() + 1;
}