mercredi 16 novembre 2016

common code practice to perform database operations in realm

This is more of a design pattern question rather than realm , I am trying to add two different types of objects into realm.

complaint 
dealer

i have created a two separate function to add these two objects. My problem is i dont want to write separate code for inserting data in to realm for each object , i want to generalize like by defining a common class and then extending that class.

here is my current code

for dealers

public void insertDealersData(final Dealers dealers){

    final Realm realm = getExplicitRealm() ;
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {

            int newDealersSize = dealers.getDealersLists().size();
            Dealers dbDealers=   realm.where(Dealers.class).findFirst();

            if(dbDealers!=null){
                for(int i =0;i<newDealersSize;i++){
                    DealersList temp=   realm.where(DealersList.class).equalTo("dealerid",
                            dealers.getDealersLists().get(i).getDealerid()).findFirst();
                    if(temp!=null){
                        dbDealers.getDealersLists().set(i,temp);
                    }else{
                        dbDealers.getDealersLists().add( dealers.getDealersLists().get(i));
                    }
                }
            }else{
                realm.copyToRealmOrUpdate(dealers);
                Log.i(TAG, "insertDealersData : dump");
            }
        }
    },new Realm.Transaction.Callback(){

        @Override
        public void onSuccess() {
            realm.close();
        }
        @Override
        public void onError(Exception e ) {
            realm.close();       
        }
    });
}

for complaint public void insertComplaintData(final Complaint complaint){

    final Realm realm = getExplicitRealm() ;

    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {

            int newComplaintSize = complaint.getComplaintLists().size();
            Complaint dbComplaint=   realm.where(Complaint.class).findFirst();

            if(dbComplaint!=null){
                for(int i =0;i<newComplaintSize;i++){
                    ComplaintList temp=   realm.where(ComplaintList.class).equalTo("incidentid",
                            complaint.getComplaintLists().get(i).getIncidentid()).findFirst();
                    if(temp!=null){
                        dbComplaint.getComplaintLists().set(i,temp);
                        Log.i(TAG, "insertComplaintData : update existing");
                    }else{
                        dbComplaint.getComplaintLists().add(complaint.getComplaintLists().get(i));
                        Log.i(TAG, "insertComplaintData : add new ");
                    }
                }

            }else{
                realm.copyToRealmOrUpdate(complaint);
                Log.i(TAG, "insertComplaintData : dump");
            }
        }
    },new Realm.Transaction.Callback(){
        @Override
        public void onSuccess() {
            realm.close(); 
        }
        @Override
        public void onError(Exception e ) {
            realm.close();
        }
    });
}

as you can see there is no change in a code except the object type. so can any one suggest how can i generalize a code by reducing the redundancy.

Aucun commentaire:

Enregistrer un commentaire