ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-Realm返回对象的空列表

2019-10-12 19:03:04  阅读:264  来源: 互联网

标签:android java realm


我想从农民对象中获取CropDataList.当我获取Farmer Object时,它不为null,但与Farmer Object相关联的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 {

                }
            }
        } );
    }

解决方法:

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

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

标签:android,java,realm
来源: https://codeday.me/bug/20191012/1902507.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有