我剛剛開始使用RxJava,我有一個問題,我需要從2個不同的firebase json數組中獲取數據。這裏是我的JSON文件:Android自定義RxJava多次調用
{ "userId" : {
"category_list" : [ {
"cat_name" : "Contacts"
}, {
"cat_name" : "Home"
}]}}
{ "Shopping" : {
"category_list" : [ {
"cat_name" : "Clothes"
}, {
"cat_name" : "Equipment"
}]}}
這裏是我的兩個JSON
public class CategoryList implements Serializable {
private List<Category> categoryList;
public List<Category> getCategoryList() {
return categoryList;
}
public void setCategoryList(List<Category> categoryList) {
this.categoryList = categoryList;
}
}
這裏是改造的API調用POJO:
@PUT("/userData/{userId}.json")
Observable<CategoryList> insertData(@Path("userId") String userId, @Body CategoryList userArray);
@GET("/userData/{userId}.json")
Observable<CategoryList> getUserCategories(@Path("userId") String userId);
@GET("/adminData/Shopping.json")
Observable<CategoryList> getAdminCategories();
我已經創建一個平面地圖rxjava,它首先調用insertData從改造a第二,當調用getUserCategories來自同一個數據庫,但我也需要是調用同一個對象(POJO)上getAdminCategories並使用相同的適配器此。這意味着我需要下面的代碼來調用和合並(?)兩個數據源1 recyclerView有誰知道,我怎麼能在下面的代碼上做到這一點?這裏是代碼:
@Override
public void postArrayAndGetCategories(final String userId, CategoryList userArray) {
subscription= dataManager.insertData(userId, userArray)
.flatMap(new Func1<CategoryList, Observable<CategoryList>>() {
@Override
public Observable<CategoryList> call(CategoryList categoryList) {
// code to save data from service A to db
// call service B
return dataManager.getUserCategories(userId);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<CategoryList>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.e(TAG, e.toString());
mainView.showError(e.toString());
}
@Override
public void onNext(CategoryList categoryList) {
mainView.showCategoryList(categoryList); // here I use adapter
}
});
}
我建議你嘗試RxFirebase庫,以與RxJava和火力地堡起來較輕鬆的工作崗位:https://github.com/FrangSierra/Rx2Firebase –