如何从两个可观察量中连接数据

时间:2017-07-08 12:01:23

标签: android rx-android sqlbrite

我有两个可观察者:

 Observable <Profile> profileObservable = briteDatabase.createQuery(UserMapper.Table.NAME, sql, String.valueOf(userId)).mapToOneOrDefault(UserMapper.MAPPER, null);
 Observable <List<Car>> carObservable = briteDatabase.createQuery(CarMapper.Table.NAME, sql, String.valueOf(userId)).mapToOneOrDefault(CarMapper.MAPPER, null);

 public class Profile {
 int id;
 String name; 
 ArrayList<Car> car; 
 ...
 }

 public class Car {
 int id;
 String brand;
 ...
 }

我需要从carObservable获取汽车列表并将其放入profileObservable(将汽车列表放入Profile),内部方法如下:

    @Override
public Observable<Profile> getProfile(String userId) {
    String sql = String.format("SELECT %s FROM %s WHERE %s=?", TextUtils.join(",", UserMapper.PROJECTION), UserMapper.Table.NAME, UserMapper.Columns.ID);

    Observable <Profile> profileObservable = briteDatabase.createQuery(UserMapper.Table.NAME, sql, String.valueOf(userId)).mapToOneOrDefault(UserMapper.MAPPER, null);

    Observable <List<Car>> carsObservable = getCars(String userId);

    /*
     Some important method to concat and return Observable <Profile>. I'm only beginner at RxJava( 

   */

}

1 个答案:

答案 0 :(得分:0)

您是否尝试过concat运算符,我不知道我是否理解您的用例,但是如果您只需要查看汽车列表然后查看配置文件,只需一个接一个地连接

@Test
public void testContact() {
    Observable.concat(Observable.just("Hello"),
                      Observable.just("reactive"),
                      Observable.just("world"))
            .subscribe(System.out::println);
}

如果您想在RxJava世界中发起,请查看https://github.com/politrons/reactive