title: Subscriber onNext is called before completion of the asynchronous requests in rxjava2
body: <p>I have implemented a repository pattern in MVP using RxJava2 </p>
  
  <p><strong>RemoteDataSource.java</strong></p>
  
  <pre><code>public Observable&lt;List&lt;A&gt;&gt; getAList(){
            return ApiService.
                  getAList()
                  .compose(RxUtils.applySchedulers())
                  .doOnSubscribe(disposable -&gt; Timber.d(..))
                  .doOnError(throwable -&gt; Timber.d(..))
                  .doOnComplete(() -&gt; {
                      Timber.d(..);
                  });
        }
  </code></pre>
  
  <p><strong>LocalDataSource.java</strong></p>
  
  <pre><code>public Observable&lt;List&lt;A&gt;&gt; getAList(){
      return mDbHelper .....from SQLBrite..
  }
  
  public void saveAList(List&lt;&lt;A&gt; a){
      SQlBriteTransaction...
  }
  </code></pre>
  
  <p><strong>Repository.java</strong>  (Update)</p>
  
  <pre><code>  @Inject
    public Repository(DownloadUtils downloadUtils){
     this.mDownloadUtils = downloadUtils;
     }
  
     @Override
     public Observable&lt;List&lt;A&gt;&gt; getAList(){
       return  mRemoteDataSource
              .getAList()
               .flatMapIterable(List&lt;A&gt; -&gt; a)
              .flatMap(A a -&gt;
        ************************************************************   
                 return Observable.fromIterable(a.getB())
                       .flatMap((Function&lt;B, ObservableSource&lt;B&gt;&gt;) b -&gt;
                                         Observable.create(emitter -&gt; 
                                                  emitter.onNext(new 
              DownloadUtils().downloadFiles(b,totalListCount,emitter))))
                                  .toList()
                                  .toObservable()
  
       ***************************************************
                      .toList()
                      .toObservable()
                      .doOnNext( List&lt;A&gt;  a -&gt; {
  
       --------------Only the first change in B value is inserted in Db-
                         mLocalDataSource.saveAList(a);
                      });
      }
  </code></pre>
  
  <p><strong>DownLoadUtils.java</strong> (Update)</p>
  
  <pre><code>void downloadBFiles(B b, int totalCount,ObservableEmitter&lt;B&gt; emitter){
          fileCount = b.size;
           b.get(index).setDataToChange(dataToChange);
  
           *** I am using PR Downloader for aynchronous download using 
              RECURSION **
          PRDownloader.download(remoteUrl, filePath, fileName)
                  .build()
          .setOnStartOrResumeListener(() -&gt; {
              })
              .setOnProgressListener(progress -&gt; {
                  int progressPercent = (int) (progress.currentBytes * 
                     100 / progress.totalBytes);,
  
               })
              .start(new OnDownloadListener() {
                  @Override
                  public void onDownloadComplete() {
    ********************* emitter.onComplete() ******************
  
  
               @Override
                  public void onError(Error error) {
                    }   
       }
  </code></pre>
  
  <p><strong>Presenter.java</strong></p>
  
  <pre><code>void getVideosFromRepo(){
  
      disposable = mRepository
                   .getAList()
                   .doOnSubscribe(d _-&gt; "Started Loading")
                   .subscribe(
                     //OnNext
      ------------- Here the OnNext is being called before Asynchronous Operation completes!!-------
  
                      List&lt;A&gt; a -&gt; mView.setAList(a);
                     )
  
  
  }
  </code></pre>
  
  <p>Above presenter implementation, returns the List in the <em>onNext</em> of the <em>Presenter</em> even before asynchronous download completes... what changes are needed , so that onNext(subscribe) is called after all the download Has <em>Completed</em>.!!!</p>