<commit_before>package com.jraska.github.client.rx;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import rx.Observable;
import rx.Subscriber;
import timber.log.Timber;

public class ObservableLoadFragment<A extends FragmentActivity, R> extends Fragment {
  public static final String TAG = ObservableLoadFragment.class.getSimpleName();

  private boolean _validInstance;

  private final Observable<R> _observable;
  private final ResultDelegateProvider<A, R> _resultDelegateProvider;

  private Result<A> _result;
  private Subscriber<R> _subscriber;
  private boolean _deliverRequested;

  public ObservableLoadFragment() {
    this(null, null);

    _validInstance = false;
  }

  private ObservableLoadFragment(Observable<R> observable,
                                 ResultDelegateProvider<A, R> resultDelegateProvider) {
    _validInstance = true;

    setRetainInstance(true);

    _observable = observable;
    _resultDelegateProvider = resultDelegateProvider;
  }

  public boolean isValid() {
    return _validInstance;
  }

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (_observable == null) {
      Timber.d("Detaching useless fragment %s created by system", this);
      detachSelf();
      return;
    }

    load();
  }

  @Override
  public void onResume() {
    super.onResume();

    deliverResult();
  }

  @Override
  public void onDestroy() {
    _subscriber.unsubscribe();

    super.onDestroy();
  }

  @SuppressWarnings("unchecked") ResultDelegate<R> delegate() {
    return _resultDelegateProvider.delegate((A) getActivity());
  }
<commit_msg>Unsubscribe sibscriber only when is subscribed<commit_after>package com.jraska.github.client.rx;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import rx.Observable;
import rx.Subscriber;
import timber.log.Timber;

public class ObservableLoadFragment<A extends FragmentActivity, R> extends Fragment {
  public static final String TAG = ObservableLoadFragment.class.getSimpleName();

  private boolean _validInstance;

  private final Observable<R> _observable;
  private final ResultDelegateProvider<A, R> _resultDelegateProvider;

  private Result<A> _result;
  private Subscriber<R> _subscriber;
  private boolean _deliverRequested;

  public ObservableLoadFragment() {
    this(null, null);

    _validInstance = false;
  }

  private ObservableLoadFragment(Observable<R> observable,
                                 ResultDelegateProvider<A, R> resultDelegateProvider) {
    _validInstance = true;

    setRetainInstance(true);

    _observable = observable;
    _resultDelegateProvider = resultDelegateProvider;
  }

  public boolean isValid() {
    return _validInstance;
  }

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (_observable == null) {
      Timber.d("Detaching useless fragment %s created by system", this);
      detachSelf();
      return;
    }

    load();
  }

  @Override
  public void onResume() {
    super.onResume();

    deliverResult();
  }

  @Override
  public void onDestroy() {
    if (_subscriber != null && !_subscriber.isUnsubscribed()) {
      _subscriber.unsubscribe();
    }

    super.onDestroy();
  }

  @SuppressWarnings("unchecked") ResultDelegate<R> delegate() {
    return _resultDelegateProvider.delegate((A) getActivity());
  }

{'fraction_non_alphanumeric': 0.09494893734474193, 'fraction_numerical': 0.0011040574109853713, 'mean_word_length': 4.026352288488211, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6274780', 'n_tokens_mistral': 1046, 'n_tokens_neox': 998, 'n_words': 237}