Chaining Retrofit并仅从本地sqlite返回观察者

时间:2017-01-09 12:13:52

标签: android sqlite retrofit rx-java

我对RxJava很新,并在Android中使用它。

在我的演示者中,我正在尝试使用RxJava通过改造来获取名为赋值的数据。

算法非常简单,检查是否存储了任何本地分配,如果没有从远程服务器获取,然后再尝试从本地获取。

目前它的作用是从本地sqlite返回0结果时从远程服务器获取。我希望它只从本地sqlite返回,例如我如何调用它 mRepository.query(new AssignmentSpecification.class)再次只返回那个?

更新为当前解决方案

restartableLatestCache(REQUEST_ASSIGNMENTS,
        () -> {

            Observable<List<Assignment>> assignmentsFromLocalDb = mRepository.query(new AssignmentSpecification())
                    .flatMap(assignments -> assignments == null || assignments.isEmpty() ?
                        Observable.zip(mRetrofit.create(AssignmentService.class)
                                            .GET(mAccessToken)
                                            .flatMap(mRepository::add),
                                    mRetrofit.create(EntityService.class)
                                            .GET(mAccessToken)
                                            .flatMap(mEntityRepository::add),
                                    mRetrofit.create(FacilityService.class)
                                            .GET(mAccessToken)
                                            .flatMap(mFacilityRepository::add),
                                    (dbAssignments, remoteEntities, remoteFacilities) -> dbAssignments)
                                    .flatMap(dbAssignments ->  mRepository.query(new AssignmentSpecification()) : Observable.just(assignments));

            return assignmentsFromLocalDb
                    .subscribeOn(Schedulers.io())
                    .observeOn(mainThread());
        },
        (assignmentsFragment, response) -> assignmentsFragment.onSuccess(response),
        (assignmentsFragment, throwable) -> assignmentsFragment.onError(throwable)
    );

存储库示例

@Override
public Observable<List<Assignment>> query(Specification specification) {
    final SqlSpecification sqlSpecification = (SqlSpecification) specification;

    final SQLiteDatabase database = mOpenHelper.getReadableDatabase();
    final List<Assignment> assignments = new ArrayList<>();

    try {
        final Cursor cursor = database.rawQuery(sqlSpecification.toSqlQuery(), new String[]{});

        for (int i = 0, size = cursor.getCount(); i < size; i++) {
            cursor.moveToPosition(i);

            assignments.add(mToAssignmentMapper.map(cursor));
        }

        cursor.close();

        return Observable.just(assignments);
    } finally {
        database.close();
    }
}

1 个答案:

答案 0 :(得分:1)

假设存储库看起来像:

interface Repository {
    Observable<List<Assignment>> query(AssignmentSpecification assignmentSpecification);
    Observable<List<Assignment>> add(List<Assignment> assignments);
}

您可以使用此代码获取数据:

Observable<List<Assignment>> assigmnets = mRepository.query(new AssignmentSpecification())
        .flatMap(assignments -> assignments == null || assignments.isEmpty() ? Observable.zip(mRetrofit.create(AssignmentService.class)
                        .GET(mAccessToken)
                        .flatMap(mRepository::add),
                mRetrofit.create(EntityService.class)
                        .GET(mAccessToken)
                        .flatMap(mEntityRepository::add),
                mRetrofit.create(FacilityService.class)
                        .GET(mAccessToken)
                        .flatMap(mFacilityRepository::add),
                (apiAssignments, entities, facilities) -> apiAssignments)
                .flatMap(apiAssignments -> mRepository.query(new AssignmentSpecification())) : Observable.just(assignments));