From 4cbc32a98b88fd6d18d6263151eac23d54e65d54 Mon Sep 17 00:00:00 2001 From: Liu Ye Dong Date: Wed, 20 Jun 2018 03:51:31 +0800 Subject: [PATCH] Reduce memory (#433) * Make EpoxyModelGroup.Holder static * Clear oldStateList and oldStateMap to release old models * Make EpoxyModelGroup.Holder static * Make EpoxyModelGroup.Holder static --- .../java/com/airbnb/epoxy/DiffHelper.java | 7 +++++++ .../com/airbnb/epoxy/EpoxyModelGroup.java | 20 ++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/DiffHelper.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/DiffHelper.java index 286daae801..865d448573 100755 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/DiffHelper.java +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/DiffHelper.java @@ -189,9 +189,16 @@ private UpdateOpHelper buildDiff(UpdateOpHelper updateOpHelper) { collectMoves(updateOpHelper); collectChanges(updateOpHelper); + resetOldState(); + return updateOpHelper; } + private void resetOldState() { + oldStateList.clear(); + oldStateMap.clear(); + } + private void prepareStateForDiff() { // We use a list of the models as well as a map by their id, // so we can easily find them by both position and id diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyModelGroup.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyModelGroup.java index 774abe20d3..1d77079633 100644 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyModelGroup.java +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyModelGroup.java @@ -301,14 +301,22 @@ protected boolean useViewStubLayoutParams(EpoxyModel model, int modelPosition @Override protected final Holder createNewHolder() { - return new Holder(); + return new Holder(this); } - public class Holder extends EpoxyHolder { + public static class Holder extends EpoxyHolder { + // Hold it for `bindView`, clear it after `bindView` be called + private EpoxyModelGroup modelGroup; + private List> models; private List views; private List holders; private ViewGroup rootView; + public Holder(@NonNull EpoxyModelGroup modelGroup) { + this.modelGroup = modelGroup; + this.models = modelGroup.models; + } + /** * Get the root view group (aka * {@link android.support.v7.widget.RecyclerView.ViewHolder#itemView}. @@ -337,7 +345,8 @@ protected void bindView(View itemView) { EpoxyModel model = models.get(i); View view; if (useViewStubs) { - view = replaceNextViewStub(childContainer, model, useViewStubLayoutParams(model, i)); + view = replaceNextViewStub(childContainer, model, + modelGroup.useViewStubLayoutParams(model, i)); } else { view = createAndAddView(childContainer, model); } @@ -352,6 +361,11 @@ protected void bindView(View itemView) { views.add(view); } + + // Clear the reference, because of `EpoxyController.buildModels` + // will build new modelGroup instance during every call. + modelGroup = null; + models = null; } /**