2017-05-05 76 views
0

我想保存從API獲得的數據,我的功能如下 嘗試{/或模擬網絡訪問。 mNetworkSubscription = NetworkRequest.performAsyncRequest(api.getPatientsData(tenantID),(數據) - > {// 更新UI的主線程 嘗試{// 長pat_id = 0;如果 (數據= NULL){ 如果! (data.getAsJsonObject()得到( 「錯誤」)!= NULL){ publishResults( 「getPatientsData」,STATUS_ERROR,NULL);} 境界不保存數據

    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); 
    } 

但realm.copyOrUpdate(patientRecords);

未在本地創建/保存記錄。

+0

你肯定沒有arised例外? –

回答

0

首先確認,如果沒有例外出現?

嘗試以下解決方法,

  1. 檢查casesJsonArray.size() > 0必須是真實的。 (OR)
  2. 刪除realm.close()並嘗試一次。有時它會在操作之間關閉領域數據庫連接。 (OR)
  3. 嘗試使用硬編碼(虛擬)數據。

如果上述不起作用,然後在此處張貼您的患者模型

注:對於ID,您可以使用下面的代碼,這將減少沒有數據庫的calls-的自動遞增

Number num = realm.where(Patient.class).max("id"); 
int l_id; 
if (num == null) { 
    i_id = 0; 
} else { 
    i_id = num.intValue() + 1; 
}