如何从rxjava链调用不可观察的代码

如何从rxjava链调用不可观察的代码,java,android,sqlite,rx-java,Java,Android,Sqlite,Rx Java,我在presenter中实现了一个rxjava调用链。 它所做的是,若sqlite数据库并没有返回任何结果,则调用远程服务器进行改造 然而,除了我们调用sqlite的存储库之外,所有东西都在使用rxjava。当sqlite返回一个结果时,它会向我显示例如3个带有空值的结果。从rxjava链调用不可观察的代码似乎不起作用?如果不使用StorIO或Brite,我怎么做 结果是这样的 演讲者 存储库 SQLite本地数据存储 使用Observable.create将查询更改为如下内容。这种方法的另一个

我在presenter中实现了一个rxjava调用链。 它所做的是,若sqlite数据库并没有返回任何结果,则调用远程服务器进行改造

然而,除了我们调用sqlite的存储库之外,所有东西都在使用rxjava。当sqlite返回一个结果时,它会向我显示例如3个带有空值的结果。从rxjava链调用不可观察的代码似乎不起作用?如果不使用StorIO或Brite,我怎么做

结果是这样的

演讲者

存储库

SQLite本地数据存储

使用Observable.create将查询更改为如下内容。这种方法的另一个变体是为每条记录返回Observable和call subscriber.onNext

@Override 
public Observable<List<Assignment>> query(Specification specification) {
    return Observable.create(subscriber -> {
            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));
                } 

                subscriber.onNext(assignments);

                cursor.close();
            } finally { 
                database.close();
                subscriber.onCompleted(); 
            } 
    }
} 

您是否尝试提取DB并检查值?是的,我尝试了,并且它们存储正确。是否可以使用Observable.defer运算符?怎么做我来试试你的解决办法。效果很好!:现在SQLite抛出锁定异常有问题,但我想这是一个新问题…'
return mAssignmentLocalDataStore.query(new AssignmentSpecification())
            .flatMap(assignments -> assignments == null || assignments.isEmpty() ?
                    mAssignmentRemoteDataStore.query()
                        .flatMap(remoteAssignments ->
                            Observable.zip(
                                    mEntityRepository.query()
                                            .flatMap(mEntityRepository::add),
                                    mFacilityRepository.query()
                                            .flatMap(mFacilityRepository::add),
                                    mAssignmentLocalDataStore.query(new AssignmentSpecification()),
                                    (remoteEntities, remoteFacilities, assignmentsRetry) -> assignmentsRetry
                            )
                        ): Observable.just(assignments)
            );
@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();
    }
}
@Override 
public Observable<List<Assignment>> query(Specification specification) {
    return Observable.create(subscriber -> {
            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));
                } 

                subscriber.onNext(assignments);

                cursor.close();
            } finally { 
                database.close();
                subscriber.onCompleted(); 
            } 
    }
}