rxjava mSubscription.unsubscribe onCreate是否与Activity中的onDestroy相同?
它是否将以相同的方式工作,并通过将其放在onCreate中作为subscription.subscribeOn().observeOn().subscribe().unsubscribe来取消订阅以防止内存泄漏
因为经常会调用Activity来填充下一个片段,然后弹出堆栈。如果mSubscription.unsubscribe在Activity的onDestroy中完成,则会给出RunTimeException:由于NullPointerException而无法破坏活动
我在这里错过了一些生命周期问题吗?


谢谢!

    private ViewPager viewPager;
    private TabLayout tabLayout;
    private int mPosition;
    private String id;
    private Subscription mSubscription;
    private Context mContext;
    private RealmResults<Experience> realmResults;
    private String animateExtraColumnName, tag;
    private Boolean automaticUpdate, animateResults;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_employment);

        User currentUser = AppDelegate.getCurrentUser();
        assert currentUser != null;
        mEmployee = getRealm().where(Employee.class).equalTo("user.id", currentUser.getId()).findFirst();


            //Server returns Experience's ID
            if (employeeExperience == null) {

                AccessToken accessToken = currentUser.getAccessTokenAsTokenType(AppDelegate.getContext(), "Bearer");
               employeeExperienceAPIOAuth2Call apiCall = new employeeExperienceAPIOAuth2Call(AppDelegate.getContext(), accessToken);

                Observable<ResponseEmployeeExperience> postEmployeeExperienceObservable = apiCall.postEmployeeExperiencesRX();

                mSubscription = postEmployeeExperienceObservable
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe((data) -> {
                            Log.d(TAG, "onCreate: Subscribe ");
                         employeeExperience = data.getEmployeeExperience();
                            Log.d(TAG, "onCreate: employeeExperience.getId()");

                            getRealm().beginTransaction();
                            getRealm().copyToRealmOrUpdate(employeeExperience);
                            // This adds the new employeeExperience object into the EmployeeExperiences RealmList
                            employee.employeeExperiences.add(employeeExperience);
                            getRealm().commitTransaction();
                            getRealm().close();

                            //unsubscribe here because the activity doesnt close itself to begin with?
                            mSubscription.unsubscribe();
                            Log.d(TAG, "onCreate: Unsubscribe: ");

                            // prepare data needed by our fragment
                            FragmentAddOrUpdateEmploymentHistory addEmploymentHistoryFragment = new FragmentAddOrUpdateEmploymentHistory();

                            FragmentManager fm = getSupportFragmentManager();
                            fm.beginTransaction().replace(R.id.edit_frame_layout, addEmploymentHistoryFragment, "EDIT_FRAGMENT").commit();

                        }, (error) -> {
                            // TODO do whatever error handling we need to do here
                            Log.d(TAG, "onCreate: postEmployeeExp error");
                            error.printStackTrace();
                        });
            }

        } else if (employee.getEmployeeExperiences() != null) {
            employeeExperience = getRealm().where(employeeExperience.class).equalTo("id", id).findFirst();

            FragmentAddOrUpdateEmploymentHistory addEmploymentHistoryFragment = new FragmentAddOrUpdateEmploymentHistory();

            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().replace(R.id.edit_frame_layout, addEmploymentHistoryFragment, "EDIT_FRAGMENT").commit();
        }

        ImageButton checkButton = (ImageButton) findViewById(R.id.edit_toolbar_check);
        checkButton.setOnClickListener(this);

        ImageButton crossButton = (ImageButton) findViewById(R.id.edit_toolbar_cross);
        crossButton.setOnClickListener(this);

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onClick(View view) {

        Fragment editFragments = getSupportFragmentManager().findFragmentByTag("EDIT_FRAGMENT");

        switch (view.getId()) {

            case R.id.edit_toolbar_check:
                if (editFragments instanceof FragmentEditDate) {
                    Log.d(TAG, "onClick: editdate ");
                    getSupportFragmentManager().popBackStack();
                } else if (editFragments instanceof FragmentSalary){
                    Log.d(TAG, "onClick: salary ");
                    getSupportFragmentManager().popBackStack();
                } else if (editFragments instanceof FragmentAddOrUpdateEmploymentHistory) {
                    Log.d(TAG, "onClick: employmenthistory ");
                    finish();
                } else if (editFragments instanceof FragmentSingleChoice) {
                    Log.d(TAG, "singlechoice  " );
                    getSupportFragmentManager().popBackStack();
                }
                else if (editFragments instanceof FragmentMultipleChoice) {
                    Log.d(TAG, "multiplechoice  " );
                    getSupportFragmentManager().popBackStack();
                }
                break;


            case R.id.edit_toolbar_cross:
                if (editFragments instanceof FragmentAddOrUpdateEmploymentHistory) {

                            getRealm().beginTransaction();
                            employeeExperience.DELETE();
                            getRealm().commitTransaction();
                            getRealm().close();
                        }
                        finish();
                    } else {
                        finish();

                    }
                } else {
                    getSupportFragmentManager().popBackStack(); }
                break;

        }
    }

最佳答案

简而言之-订阅Observable后,您就可以接收来自它的通知并对其作出反应(onNextonErroronComplete)。同样,在大多数情况下(对于冷的可观察物),这实际上是Observable开始发射物品的触发点。

来自Observable的Unsubscribing表示您不再有兴趣接收任何进一步的通知。当您不再想要接收任何东西时,您需要unsubscribe,这与在Activity的onDestroy()方法中释放任何资源的方式类似。当然,与活动的生命周期相似只是为了进行比较-订阅/取消订阅过程是完全独立的。

您可以阅读有关subscribingunsubscribing含义的详细信息[HERE] [1] [1]:http://reactivex.io/documentation/contract.html

也就是说,对您的代码的一些具体建议:

您目前在Android的主线程上执行了太多操作。我建议将所有与实物相关的内容作为RX链的一部分,而不是在onNext()中的subscribe(...)处理程序中移动。这样,它将在io线程上执行。就像是:

...
mSubscription = postEmployeeExperienceObservable
            .subscribeOn(Schedulers.io())
            .doOnNext(data -> saveDataToRealm(data))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe((data) -> {
                // prepare data needed by our fragment
                FragmentAddOrUpdateEmploymentHistory addEmploymentHistoryFragment = new FragmentAddOrUpdateEmploymentHistory();

                FragmentManager fm = getSupportFragmentManager();
                fm.beginTransaction().replace(R.id.edit_frame_layout, addEmploymentHistoryFragment, "EDIT_FRAGMENT").commit();

            }, (error) -> {
                // TODO do whatever error handling we need to do here
                Log.d(TAG, "onCreate: postEmployeeExp error");
                error.printStackTrace();
            });
}

private Data saveDataToRealm(Data data) {
    employeeExperience = data.getEmployeeExperience();
    Log.d(TAG, "onCreate: employeeExperience.getId()");

    getRealm().beginTransaction();
    getRealm().copyToRealmOrUpdate(employeeExperience);
    // This adds the new employeeExperience object into the EmployeeExperiences RealmList
    employee.employeeExperiences.add(employeeExperience);
    getRealm().commitTransaction();
    getRealm().close();
}


就取消订阅而言-如果您真的想从unsubscribe事件中进行onNext()预订,一种更好的策略是通过Subscriber<Data>进行订阅,然后使用它的unsubscribe()方法。换句话说-您不会呼叫mSubscription.unsubscribe(),而是呼叫this.unsubscribe()

mSubscription = postEmployeeExperienceObservable
            .subscribeOn(Schedulers.io())
            .doOnNext(data -> saveDataToRealm(data))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<Data>() {

                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(Data data) {
                    ...
                    this.unsubscribe();
                }
            });


综上所述,我认为您并不是真的需要使用unsubscribe方法中的onNext(),而是可以在onDestroy()中这样做,当然可以检查先前是否创建了预订:

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mSubscription != null) {
        mSubscription.unsubscribe();
    }
}

10-08 16:41