1

我已经在我的应用程序中实现了 MVP 模式。我正在使用 Wea​​kReferences 将 View 的引用存储在我的 Presenter 中。但是我的碎片在销毁时仍然没有被 GC 认领。下面是问题的截图。知道是什么原因造成的以及如何解决这个问题吗?

Android Profiler 的屏幕截图

下面是我的 Presenter 的代码:

public class ProductDetailPresenter implements ProductDetailContract.Presenter {

private final WeakReference<ProductDetailContract.View> view;
private CategoriesDataSource repo;

public ProductDetailPresenter(ProductDetailContract.View view, CategoriesDataSource repo) {
    this.view = new WeakReference<>(view);
    this.repo = repo;
    view.setPresenter(this);
}

@Override
public void start() {

}

@Override
public void submitRating(final Product product, final float mRating) {
    final ProductDetailContract.View view =ProductDetailPresenter.this.view.get();

    if (view != null) {
        repo.submitRating(product.getId(), mRating, true, new CategoriesDataSource.SubmitRatingCallback() {

            @Override
            public void onRatingSubmitted() {

                product.setRating(mRating);
                product.setRated(true);
                product.setUpdatedAt(new Date(System.currentTimeMillis()));
                repo.updateProductInDB(product);

                if (!view.isActive()) return;

                view.onRatingSubmitted(true, mRating);
            }

            @Override
            public void onError(Throwable throwable) {
                if (!view.isActive()) return;

                view.onRatingSubmitted(false, 0);
            }
        });
    }
}

@Override
public void onRateKarenClicked() {
    ProductDetailContract.View view = this.view.get();
    if (view != null) {
        view.openDialog();
    }
}

@Override
public void onAbhiKhareediyeClicked(Product product) {

    EventBus.getDefault().post(
            new ProductDetailContract.ContractEventMessages(
                    ProductDetailContract.ContractEventMessages.EVENT_START_QUANTITY_SCREEN, product));

}

}

4

1 回答 1

0

这就是问题:

    @Override
    public void submitRating(final Product product, final float mRating) {
       final ProductDetailContract.View view =ProductDetailPresenter.this.view.get(); <-- this is bad

您有一个最终对象正在传递给 repo。删除整行。你不需要它。在 onRatingSubmitted 和 onError 内的 view.get() 中使用

于 2018-07-03T18:15:00.030 回答