Skip to content

Commit

Permalink
Reduce memory (#433)
Browse files Browse the repository at this point in the history
* Make EpoxyModelGroup.Holder static

* Clear oldStateList and oldStateMap to release old models

* Make EpoxyModelGroup.Holder static

* Make EpoxyModelGroup.Holder static
  • Loading branch information
baoti authored and elihart committed Jun 19, 2018
1 parent bcfecc8 commit 4cbc32a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
7 changes: 7 additions & 0 deletions epoxy-adapter/src/main/java/com/airbnb/epoxy/DiffHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 17 additions & 3 deletions epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyModelGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends EpoxyModel<?>> models;
private List<View> views;
private List<EpoxyHolder> 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}.
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 4cbc32a

Please sign in to comment.