2017-12-11 6 views
0

다음 2 개의 asynctasks를 rxjava로 변환하려고 시도하고 있지만 어떻게해야하는지 잘 모릅니다. 어떤 아이디어? :asynctask를 Rxjava로 변환하려면 어떻게해야합니까?

new AsyncTask<Void, Void, Void>() { 
       @Override 
       protected Void doInBackground(Void... params) { 

        /* Shutdown video players */ 
        Set<Map.Entry<String, PlayerBundle>> entries = videoPlayerPool.entrySet(); 
        for (Map.Entry<String, PlayerBundle> entry : entries) { 
         PlayerBundle bundle = entry.getValue(); 
         bundle.player.release(); 
        } 

        /* Shutdown audio players */ 
        entries = audioPlayerPool.entrySet(); 
        for (Map.Entry<String, PlayerBundle> entry : entries) { 
         PlayerBundle bundle = entry.getValue(); 
         bundle.player.release(); 
        } 

        videoStreamer.stopStream(); 
        videoStreamer.release(); 

        audioStreamer.stopStream(); 
        audioStreamer.release(); 

        return null; 
       } 

       @Override 
       protected void onPostExecute(Void aVoid) { 
        cb.event(new Spin.Event<Void>()); 
       } 
      }.execute(); 

과 :

new AsyncTask<Void, Void, Void>() { 
      @Override 
      protected Void doInBackground(Void... params) { 
       Set<Map.Entry<String, Participant>> entries = pool.entrySet(); 
       for (Map.Entry<String, Participant> entry : entries) { 
        Participant participant = entry.getValue(); 
        participant.release(); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(Void aVoid) { 
       cb.event(new Spin.Event<Void>()); 
      } 
     }.execute(); 

나는 이미 내 Gradle을에서 rxjava을 포함하지만,이

+0

이 https를 시도 변환하는 방법에 대한 이동하는 방법을 잘하지 않은 : // piercezaifman합니다. co.kr/converting-an-asynctask-to-rxjava /를 읽고 문제가 발생할 때 진짜 질문을하십시오. 키워드 "TRY" –

답변

1
Observable.defer(new Func0<Observable<Void>>() { 
     @Override 
     public Observable<Void> call() { 
      /* Shutdown video players */ 
      Set<Map.Entry<String, PlayerBundle>> entries = videoPlayerPool.entrySet(); 
      for (Map.Entry<String, PlayerBundle> entry : entries) { 
       PlayerBundle bundle = entry.getValue(); 
       bundle.player.release(); 
      } 

      /* Shutdown audio players */ 
      entries = audioPlayerPool.entrySet(); 
      for (Map.Entry<String, PlayerBundle> entry : entries) { 
       PlayerBundle bundle = entry.getValue(); 
       bundle.player.release(); 
      } 

      videoStreamer.stopStream(); 
      videoStreamer.release(); 

      audioStreamer.stopStream(); 
      audioStreamer.release(); 

      return Observable.just(null); 
     } 
    }) 
    .doOnCompleted(new Action0() { 
     @Override 
     public void call() { 
      cb.event(new Spin.Event<Void>()); 
     } 
    }) 
    .subscribeOn(Schedulers.computation()) 
    .subscribe(); 

    Observable.defer(new Func0<Observable<Void>>() { 
     @Override 
     public Observable<Void> call() { 
      Set<Map.Entry<String, Participant>> entries = pool.entrySet(); 
      for (Map.Entry<String, Participant> entry : entries) { 
       Participant participant = entry.getValue(); 
       participant.release(); 
      } 
      return Observable.just(null); 
     } 
    }).doOnCompleted(new Action0() { 
     @Override 
     public void call() { 
      cb.event(new Spin.Event<Void>()); 
     } 
    }) 
    .subscribeOn(Schedulers.computation()) 
    .subscribe(); 
+0

위의 2는 2 개의 다른 asynctasks입니다. 그것들을 따로 할 수 있습니까? 또한, whats func0 및 action0. 익명의 수업처럼 그들을 할 수 있습니까? 또는 독립형 –

+0

두 번째 작업이 동일한 방식으로 변환됩니다. Func0과 Action0은 익명 클래스입니다. 인터페이스 이름은 – Aleksey

+0

입니다. 오류가 발생합니다 : 기호 func0과 Action0을 해결할 수 없습니다. 어떤 생각이 어떻게 해결할 수 있습니까? –

관련 문제