ReactiveX / RxJava

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
Apache License 2.0
47.91k stars 7.6k forks source link

how to implement the code style of rxjava #5259

Closed weituotian closed 7 years ago

weituotian commented 7 years ago

i am using greendao with rxjava, i am confused about how to implement the code style of rxjava.

you can see the Code nesting below.

        private HistoryVideoDao historyVideoDao;
        QueryBuilder<HistoryVideo> query;

        historyVideoDao = daoSession.getHistoryVideoDao();
        query = historyVideoDao.queryBuilder().orderAsc(HistoryVideoDao.Properties.ViewTime).limit(10);

        Observable.concat(historyVideoDaoRx.insert(historyVideo), historyVideoDaoRx.count())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        if (o instanceof Long) {
                            if (((Long) o) > 60) {
                                query.rx().list().observeOn(AndroidSchedulers.mainThread())
                                        .subscribe(new Action1<List<HistoryVideo>>() {
                                            @Override
                                            public void call(List<HistoryVideo> historyVideos) {
                                                historyVideoDaoRx.deleteInTx(historyVideos).subscribe(new Action1<Void>() {
                                                    @Override
                                                    public void call(Void aVoid) {

                                                    }
                                                });
                                            }
                                        });
                            }
                        }
                    }
                });

the code above mean that when i insert an history by dao object , the i check the total count in table haven more than 60 if does , i will delete 10 in table for that , i must also to query the last 10 in table and then delete the 10 in table. the insert, count , query, delete operation all are rx!

i can not make it beautiful more! how can i acheieve the goal to implement the code style of rxjava?

akarnokd commented 7 years ago

This type of question is better asked on StackOverflow.

weituotian commented 7 years ago

@akarnokd

i improve the code style below, is that rxjava Recommend?

but i have not test yet

historyVideoDaoRx.insert(historyVideo)
                .flatMap(new Func1<HistoryVideo, Observable<Long>>() {
                    @Override
                    public Observable<Long> call(HistoryVideo historyVideo) {
                        return historyVideoDaoRx.count();
                    }
                })
                .flatMap(new Func1<Long, Observable<List<HistoryVideo>>>() {
                    @Override
                    public Observable<List<HistoryVideo>> call(Long aLong) {
                        if (aLong > 60) {
                            return query.rx().list();
                        }
                        return Observable.never();
                    }
                })
                .flatMap(new Func1<List<HistoryVideo>, Observable<Void>>() {
                    @Override
                    public Observable<Void> call(List<HistoryVideo> historyVideos) {
                        return historyVideoDaoRx.deleteInTx(historyVideos);
                    }
                })
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {

                    }
                });
akarnokd commented 7 years ago

Unless you contribute to RxJava itself, you are bound to the style of your development group/company. Usually it's not a beauty contest and what matters is that the code works or not and you don't commit any of the coding sins (such as subscribing to an Observable from within onNext and such).

weituotian commented 7 years ago

@akarnokd ok ! my code work well. but i really not understand how to make it beauty