Rx android vs mosby data loading data processing

I am working on an Android application. The code I attach creates a recyclerview. The very first thing we do is create an synthase that will extract data from the SQLite database and load it into adapter-> recylcerview. While the background task is running, a progress dialog is displayed to the user.

public class HomeActivity extends AppCompatActivity
{
    private RecyclerView recycler;
    private RecyclerViewAdapter adapter;
    private SwipeRefreshLayout swipeRefresh;
    private progressDialog progressDialog;

     // ... some code here

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // ... some code here


    createRecyclerView();
    loadRecyclerView();


    // ... some code here

    }


    private void loadRecyclerView()
    {
        new LoadingBackgroundTask().execute();
    }



    private void createRecyclerView()
{

    Context context = getApplicationContext();

    recycler = (RecyclerView) findViewById(R.id.recycle_view_home);
    recycler.setHasFixedSize(true);

    RecyclerView.LayoutManager lManager = new LinearLayoutManager(context);
    recycler.setLayoutManager(lManager);

    adapter = new RecyclerViewAdapter();

    recycler.setAdapter(adapter);
    recycler.setItemAnimator(new DefaultItemAnimator());

}

private class LoadingBackgroundTask extends AsyncTask<Void, Void, List<items>> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = ProgressDialog.show(HomeActivity.this, getString(R.string.dialog_load_list),getString(R.string.dialog_please_wait), false, false);

    }

    @Override
    protected List doInBackground(Void... params) {

        List<items> lists;
        //Data Source Class ( SQLite)
        ListDS listDS = new ListDS(getApplicationContext());
        list = listDS.getList();

        return list;
    }

    @Override
    protected void onPostExecute(List result) {
        super.onPostExecute(result);

        //it inserts de list on recyclerview performing animation
        adapter.animate(result);

        progressDialog.dissmiss();
        swipeRefresh.setRefreshing(false);
        recycler.scrollToPosition(0);
    }

}

}

So far so good. However, as you probably know, this code has some known problems; for example, if I rotate the screen while asintext is doing its magic, this will crash the application.

I tried the alternative I saw in Googling, rxandroid.

(Sorry, if I typed something wrong, I do it from memory)

public class HomeActivity extends AppCompatActivity
{
 private Subscriber suscriptor;
private progressDialog progressDialog;

 //some code ....

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    suscriptor = new Subscriber() {
        @Override
        public void onCompleted() {
            progressDialog.dismiss();
            Log.d("SUSCRIPTOR","ON COMPLETE");
        }

        @Override
        public void onError(Throwable e) {
            Log.d("SUSCRIPTOR","ON ERROR");
        }

        @Override
        public void onNext(Object o) {
            adapter.animate((List<items>)o);

        }
    };


    Observable.create(
            new Observable.OnSubscribe<List<items>>() {
                @Override
                public void call(Subscriber<? super List<items>> sub) {
                progressDialog = ProgressDialog.show(HomeActivity.this,  getString(R.string.dialog_load_list),getString(R.string.dialog_please_wait), false, false);
                    List<items> lists;
                    //Data Source Class ( SQLite)
                    ListDS listDS = new ListDS(getApplicationContext());
                    list = listDS.getList();

                    sub.onNext(list);
                    sub.onCompleted();

                }

                @Override
                protected void finalize() throws Throwable {
                    super.finalize();
                    Log.d("OBSERAVBLE","FINALIZED");
                }
            })
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.newThread())
           .cache()
    .subscribe(suscriptor);

    }


@Override
public void onDestroy()
{
    if(suscriptor!=null)
    {
        if(!suscriptor.isUnsubscribed())
        {
            suscriptor.unsubscribe();
        }
    }

    super.onDestroy();
}

}

. , , , , , . , , , .

, , " ", , , . , , , , , rxandroid .

, , , , ? , , , ? , , TedMosby?

+4
3

- M odel- V iew- P resenter (MVP). , , "ted mosby pattern", MVP.

, MVP. , , . MVP - . View , Presenter , .. : , .. , . RxJava Observable. , , , . 3 ( -) Presenter View. , ( progressbar), - , ( -). MVP .

, : AsyncTask RxJava . http . , (, , ).

MVP . , HTTP- (.. ), android.

+1

, , , , AsycTask RecyclerView, setRetainInstance(true) onCreateView() .

setRetainInstance(true) , .

+1

Observable . :

  • subscriber.isUnsubscribed. "" .
  • . , , ..

:

Observable.create(
        new Observable.OnSubscribe<List<items>>() {
            @Override
            public void call(Subscriber<? super List<items>> sub) {
                sub.add(Subscriptions.create(new Action0() {
                    @Override
                    public void call() {
                        cancelLongRunningOperationIfItStillRunning();
                    }
                }));
                if (!sub.isUnsubscribed()) {
                    //start long running operation here
               }
            }
        })
        .doOnSubscribe(new Action0() {
            @Override
            public void call() {
            }
        })

// .. Observable. doOnSubscribe, .

0

Source: https://habr.com/ru/post/1626644/


All Articles