【问题标题】:Realm returns empty list of object领域返回对象的空列表
【发布时间】:2019-05-09 05:53:25
【问题描述】:

我想从 Farmer 对象中获取 CropDataList。当我获取 Farmer 对象时,它不为空,但与 Farmer 对象关联的cropData 列表返回空。我可以通过 Stetho 查看数据库条目,并且列表中有一个条目。这是我的代码。

public class Farmer extends RealmObject {

    @PrimaryKey
    private String id;
    private RealmList<CropData> cropData;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public RealmList<CropData> getCropData() {
        return cropData;
    }

    public void setCropData(RealmList<CropData> cropData) {
        this.cropData = cropData;
    }

    public static class Constants {
        public static final String FARMER_ID = "id";
    }
}

这是我的 CropData.class

public class CropData extends RealmObject {

    private String cropName;
    private String crop;
    private Float cropAcres;
    private Float cropYield;
    private Float cropPrice;

    public String getCropName() {
        return cropName;
    }

    public void setCropName(String cropName) {
        this.cropName = cropName;
    }

    public String getCrop() {
        return crop;
    }

    public void setCrop(String crop) {
        this.crop = crop;
    }

    public Float getCropAcres() {
        return cropAcres;
    }

    public void setCropAcres(Float cropAcres) {
        this.cropAcres = cropAcres;
    }

    public Float getCropYield() {
        return cropYield;
    }

    public void setCropYield(Float cropYield) {
        this.cropYield = cropYield;
    }

    public Float getCropPrice() {
        return cropPrice;
    }

    public void setCropPrice(Float cropPrice) {
        this.cropPrice = cropPrice;
    }
}

当我尝试通过以下方式获取列表时,列表为空:

    HashMap<String, String> credentials = QueryUtils.getCredentials( realm );
    Farmer farmer = realm.where( Farmer.class ).equalTo( Farmer.Constants.FARMER_ID, credentials.get( "farmerId" ) ).findFirst();
    // this farmer is not null, but associated cropData is empty...
    if (farmer != null) {
        RealmList<CropData> farmerCropData = farmer.getCropData();
        **// this list is empty...**
        Log.d( TAG, "getCropList: " + GsonUtils.toGson( farmerCropData ) );

这就是我插入cropData的方式。

public void updateCrop(String authToken, String farmerId, CropDataUpdateRequest cropDataUpdateRequest, Context context) {
        EndPoints.updateCrop( authToken, farmerId, cropDataUpdateRequest, new Callback<BasicResponse>() {
            @Override
            public void onResponse(@NonNull Call<BasicResponse> call, @NotNull Response<BasicResponse> response) {
                if (response.isSuccessful()) {
                    BasicResponse basicResponse = response.body();
                    assert basicResponse != null;
                    ErrorCode errorCode = basicResponse.getErrorCode();
                    if (!NetworkErrorHandlingUtils.ErrorCheck( errorCode )) {
                        UpdateCropResponse updateCropResponse = GsonUtils.fromGson( basicResponse.getResponse(), UpdateCropResponse.class );
                        try (Realm realm = Realm.getDefaultInstance()) {

                            //  update verification Response...
                            realm.executeTransactionAsync( realm1 -> realm1.insertOrUpdate( updateCropResponse.getUpdatedCrop() ) );

                            GetComparisonSheetRequest getComparisonSheetRequest = new GetComparisonSheetRequest();
                            CropData cropData = updateCropResponse.getUpdatedCrop().get( 0 );
                            if (cropData != null) {
                                getComparisonSheetRequest.setCrop( Crop.valueOf( cropData.getCrop() ) );
                            }
                            getIncomeComparisonSheet( authToken, farmerId, getComparisonSheetRequest, context );
                            getYieldComparisonSheet( authToken, farmerId, getComparisonSheetRequest, context );
                            getRecommendation( authToken, farmerId, context );

                            IntentUtils.PassIntent( context, HomeScreenActivity.class );
                            ((Activity) context).finish();
                        }
                    } else {
                        //  show error dialog here..., the error could be from one of the Error Code...

                    }
                }
            }

            @Override
            public void onFailure(@NonNull Call<BasicResponse> call, @NotNull Throwable t) {
                //  show UI for API Failure...
                if (t instanceof NoConnectivityException) {
                    //  This is where it throws No Internet connectivity error...
                    //  show some UI here, sefu...
                    IntentUtils.PassIntent( context, NetworkErrorActivity.class );
                }
                //  there's some other error, not network connectivity issue...
                else {

                }
            }
        } );
    }

【问题讨论】:

  • 好的……然后?我没有看到您设置值的任何代码,那么我为什么要假设有一个呢?你调试过你的代码吗?
  • @Stultuske,我不会在此处发布该代码,因为我不想让您在这方面经历麻烦。它插入数据。我可以通过听诊器验证它。
  • 当然。并且您是否验证了数据已插入正确的实例?您是否验证过插入是在获取之前完成的?您确定 findFirst 会为您提供您实际正在寻找的实例吗?
  • 是的,它给了我不为空的农民对象,通过它我正在访问cropdataList,但它是空的。是的,在我查询之前插入了数据。
  • 我不是在问农夫对象是否为空。那是完全不同的事情。

标签: java android realm


【解决方案1】:

好的,所以我找到了答案,我已将代码更改如下。我正在获取我的列表,然后将其清除,然后将新数据添加到该列表中并更新我的农民对象。

RealmList<CropData> cropDataRealmList = new RealmList<>();
cropDataRealmList.addAll( updateCropResponse.getUpdatedCrop() );

Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();

Farmer farmer = realm.where( Farmer.class )
    .equalTo( Farmer.Constants.FARMER_ID, farmerId )
    .findFirst();

if (farmer != null) {

    RealmList<CropData> farmerCropData = farmer.getCropData();
    farmerCropData.clear();
    farmerCropData.addAll( cropDataRealmList );

    CropData cropData = updateCropResponse.getUpdatedCrop().get( 0 );
    if (cropData != null) {
        GetComparisonSheetRequest getComparisonSheetRequest = new GetComparisonSheetRequest();
        getComparisonSheetRequest.setCrop( Crop.valueOf( cropData.getCrop() ) );

        getIncomeComparisonSheet( authToken, farmerId, getComparisonSheetRequest, context );
        getYieldComparisonSheet( authToken, farmerId, getComparisonSheetRequest, context );
        getRecommendation( authToken, farmerId, context );
    }
    realm.copyToRealmOrUpdate( farmer );
    realm.commitTransaction();
    realm.close();
    IntentUtils.PassIntent( context, HomeScreenActivity.class );
    ((Activity) context).finish();
}

【讨论】:

  • 所以如果farmer == null,你让领域实例保持打开状态?这可能会导致以后出现问题。
猜你喜欢
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多