RxGroups

輕鬆將 RxJava Observables 分組並將其綁定到您的 Android Activity 生命周期
696
作者Felipe Lima

RxGroups 讓您可以將 RxJava 的 Observable 依照群組分組,並將它們綁定到您的 Android 生命周期。這在搭配 Retrofit 使用時特別有用。

對於簡單的情境,您或許可以讓原始請求被取消並發送一個新的請求。然而,不難看出這在更複雜的情況下會成為問題。

假設您的使用者正在提交付款。您可能希望確保在旋轉螢幕或離開 Activity 並稍後返回後,可以重新連接到相同的進行中或已完成的請求。

RxGroups 還會自動阻止事件在您的 ActivityFragment onResume() 之前和 onPause() 之後被傳遞。如果發生這種情況,它們將自動緩存在記憶體中,並在使用者返回螢幕後立即傳遞。如果使用者從未返回,則會在 onDestroy() 後自動回收記憶體。

使用方式

  1. 在您的 ActivityFragmentDialog 等中添加 GroupLifecycleManager 欄位,並根據您自己的生命週期方法呼叫其對應的生命週期方法(例如:onPauseonResumeonDestroy 等);
  2. 使用 @AutoResubscribe 注解您的 ResubscriptionObserver,並使用 resubscriptionTag() 方法告知 RxGroups 應該使用哪個標籤將您的 Observer 自動重新連接到它的 Observable
  3. 在訂閱您的 Observable 之前,使用 observableGroup.transform() 來組成它,以定義該 Observable 的標籤;

範例

public class MyActivity extends Activity {
  private static final String OBSERVABLE_TAG = "arbitrary_tag";
  private TextView output;
  private FloatingActionButton button;
  private GroupLifecycleManager groupLifecycleManager;
  private ObservableGroup observableGroup;
  private Observable<Long> observable = Observable.interval(1, 1, TimeUnit.SECONDS);

  // The Observer field must be public, otherwise RxGroups can't access it
  @AutoResubscribe public final ResubscriptionObserver<Long> observer = new ResubscriptionObserver<Long>() {
    @Override public void onCompleted() {
      Log.d(TAG, "onCompleted()");
    }

    @Override public void onError(Throwable e) {
      Log.e(TAG, "onError()", e);
    }

    @Override public void onNext(Long l) {
      output.setText(output.getText() + " " + l);
    }

    @Override public Object resubscriptionTag() {
      return OBSERVABLE_TAG;
    }
  };

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    output = (TextView) findViewById(R.id.txt_output);
    button = (FloatingActionButton) findViewById(R.id.fab);
    SampleApplication application = (SampleApplication) getApplication();
    ObservableManager manager = application.observableManager();
    groupLifecycleManager = GroupLifecycleManager.onCreate(manager, savedInstanceState, this);
    observableGroup = groupLifecycleManager.group();

    button.setOnClickListener(v -> observable
        .compose(observableGroup.<Long>transform(OBSERVABLE_TAG))
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(observer));
  }

  @Override protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    groupLifecycleManager.onSaveInstanceState(outState);
  }

  @Override protected void onResume() {
    super.onResume();
    groupLifecycleManager.onResume();
  }

  @Override protected void onPause() {
    super.onPause();
    groupLifecycleManager.onPause();
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    groupLifecycleManager.onDestroy(this);
  }
}

可選:如果您不想使用帶有 @AutoResubscribeResubscriptionObserver,只需使用帶有名為 resubscriptionTag() 的公有方法的常規 Observer 匿名類別即可。例如:

@AutoResubscribe public final Observer<Long> observer = new Observer<Long>() {
    @Override public void onCompleted() {
    }

    @Override public void onError(Throwable e) {
    }

    @Override public void onNext(Long l) {
    }

    public Object resubscriptionTag() {
      return Arrays.asList("tag1", "tag2", "tag3");
    }
  };

如果該方法不存在或不是 public,RxGroups 將會拋出 RuntimeException 通知您。 您可以使用任何 Object 標籤作為您的 Observer,包括陣列和 List。在這種情況下,它會將 Observer 與集合中的所有標籤關聯起來,讓您可以與多個 Observables 共享同一個 Observer

使用 Gradle 下載

compile 'com.airbnb:rxgroups-android:0.3.5'

查看 範例應用程式 以獲取更多詳細資訊和完整範例!

開發版本的快照可在 Sonatype 的 snapshots 儲存庫中找到。