2017-09-14 3 views
0

카테고리 목록 (A, B, C)이 있으며 각 목록에는 하위 카테고리 (A1, A2), (B1, B2), (C1, C2) 및 각 하위 범주에는 다운로드 할 항목 목록 (item_a11, item_a12), (item_a21, item_a22), (item_b11, item_b12) 등이 있습니다. 따라서 다음 순서로 항목을 하나씩로드해야합니다.RxJava | RxAndroid 하나씩 항목로드

Loading category A 
...Loading subcategory A1 
......Loading item_a11 - check if we still have free space 
......Loading item a12 - check if we still have free space 
...Loading subcategory A2 
......Loading item_a12 - check if we still have free space 
......Loading item a12 - check if we still have free space - no space 
Download Completed 

RxJava를 사용하여 구현할 수 있습니까? 그렇다면 모든 조언에 대해 매우 감사 할 것입니다.

+0

하위 범주가 범주에로드되었거나 추가 호출이 필요합니까? – crgarridos

답변

0

처럼 뭔가를 할 수 있습니다, 당신은이 솔루션을 시도 할 수 있습니다. 하나씩 항목을 다운로드하고 공간이없는 경우 예외가 발생하므로 다운로드를 더 이상 시도하지 않습니다.

public interface Model { 

    Single<String> download(String item); 

    Single<List<Category>> categories(); 

    Single<Boolean> availableSpace(); 
} 


public class Category { 

    public List<Subcategory> subcategories; 

    public List<Subcategory> getSubcategories() { 
     return subcategories; 
    } 
} 

public class Subcategory { 

    public List<String> items; 

    public List<String> getItems() { 
     return items; 
    } 
} 


private Model model; 

public void downloadAll() { 
    model.categories() 
      .flatMapObservable(Observable::fromIterable) 
      .map(Category::getSubcategories) 
      .flatMap(Observable::fromIterable) 
      .map(Subcategory::getItems) 
      .flatMap(Observable::fromIterable) 
      .flatMapSingle(item -> model.availableSpace() 
        .flatMap(available -> { 
         if (available) { 
          return model.download(item); 
         } else { 
          return Single.error(new IllegalStateException("not enough space")); 
         } 
        })) 
      .subscribeOn(Schedulers.io()) 
      .observeOn(AndroidSchedulers.mainThread()) 
      .subscribe(item -> {}, throwable -> {}); 
} 
0

당신이 당신의 클래스가 유사하다고 가정이

1)make method that returns List of A(getListOfA). 
2)now getListofA.subscribe(). 
3)now on onNext() call getListOfA1() that return single value using fromIterable()(i.e. return single item from A1. 
4)now on getListofA1().subscribe()'s onNext you can do what you want. 
관련 문제