Realm returns empty list of object

匆匆过客 提交于 2019-12-31 02:51:05

问题


I want to fetch CropDataList from Farmer Object. when i fetch Farmer Object it's not null, but the cropData list associated with Farmer Object returns empty. I can see database entry via Stetho and there list has one entry. Here's my code.

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

This is my 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;
    }
}

The list is empty when i try to fetch it via :

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

this is how i am inserting 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 {

                }
            }
        } );
    }

回答1:


Okay, so i have find out my answer, I have changed my code as following. i am fetching my list, then clear it and then i am adding new data into that list and updating my farmer Object.

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


来源:https://stackoverflow.com/questions/56052965/realm-returns-empty-list-of-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!