weex icon indicating copy to clipboard operation
weex copied to clipboard

[BUG] Android 中List 给Cell配置了scope之后很容易发生crash

Open LeeJay0226 opened this issue 10 years ago • 2 comments

背景:在优化Android过程中发现List提供了设置scope,给不同类型Cell设置不同type的功能,以此来优化List的性能,尽可能地减少cell的创建,提高复用率。 问题:但在实际使用中发现很容易出现问题。即使同一个cell设置了同一个scope,但也会发生crash。出现问题的原因是在实际业务中一个cell中的子元素经常会设置if控制子元素的可见性。若这个cell被复用时,会导致旧的cell和新的cell中子元素数量不一致的情况,导致数组越界,而发生crash。 发生crash的代码如下:

  @Override
  public void bindData(WXComponent component) {
    if(!isLazy()) {
      if (component == null) {
        component = this;
      }
      super.bindData(component);
      int count = childCount();
      for (int i = 0; i < count; i++) {
        getChild(i).bindData(((WXVContainer)component).getChild(i));
      }
    }
  }

这行代码 getChild(i).bindData(((WXVContainer)component).getChild(i)); 这里由于this 和component可能不相同,导致他们的children也不相同,调用上面这行代码就很容易发生数组越界的crash了。

LeeJay0226 avatar Jul 25 '16 09:07 LeeJay0226

会crash的代码,滑动到最下方,然后往上滚会crash。

<template>
  <div>
    <list class="list">
      <cell class="row" repeat="{{rows}}" index="{{$index}}" scope="1"><div class="item">  <text>row {{id}}</text>  <text if="{{$index % 2 == 1}}">row2 {{id}}</text></div>
      </cell>
    </list>
  </div>
</template>

<style>
  .list {
    height:850px
  }
  .item {
    flex-direction: row;
    justify-content: space-between;
    height: 100px;
    padding:20px;
  }
</style>

<script>
  module.exports = {
    data: {
      rows:[{id: 1},{id: 2},{id: 3},{id: 4},{id: 5},{id: 6},{id: 7},{id: 8},{id: 9},{id: 10},{id: 11},{id: 12},{id: 13},{id: 14},{id: 15},{id: 16},{id: 17},{id: 18},{id: 19},{id: 20},{id: 21},{id: 22},{id: 23},{id: 24},{id: 25},{id: 26},{id: 27},{id: 28},{id: 29}
      ]
    }
  }
</script>

LeeJay0226 avatar Jul 25 '16 11:07 LeeJay0226

@LeeJay0226 目前scope使用限制还比较多,android上view的创建和layout相对耗时比较大,因此想提高list复用,要求,scope一样的cell,需要满足view层级和布局完全一致,不然,不允许设置scope相同值

zshshr avatar Jul 26 '16 11:07 zshshr