| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
eastxiang
7年前发布

利用CursorLoader让你的数据加载快人一步

   <h3><strong>引子</strong></h3>    <p>前段时间做了一个类似通讯录侧边导航功能,实现起来蛮简单,网上也有大量现成的demo,但是最近突然啃爹的测试,说有些数据量大的用户反映严重卡顿,我的实现跟网上的一样,大都千遍一律,实际问题很多</p>    <h3><strong>问题描述</strong></h3>    <p>1,关于加载延迟,没有图片可能大家没有直观的感受,大家可以想象下,就是直接加载2000条以上数据,不加排序什么,iOS是打开页面秒显,Android即使放在内存中加载,也要延迟3秒左右,不同手机性能可能有差距。</p>    <p>经过分析,可能是以下两点问题</p>    <p>1,可能是大量数据生成字母索引和排序导致的卡顿</p>    <p>2,一次性加载的数据量太多,导致展示缓慢</p>    <h3><strong>寻找解决方案</strong></h3>    <p>1,索引和排序问题 ,优化方案是存入数据库前预先生成字母索引</p>    <p>2,排序,从数据库查询出来时搜索出来时预先排序</p>    <p>3,一次性加载太多导致延迟空白,可以做一次性分页加载</p>    <p>一次性分页加载过程中如果点击页面会出现延迟进入下个界面的问题。并且左边的字母索引不能一次性全显示,于是找了下官方通讯录的源码</p>    <h3><strong>官方实现方式</strong></h3>    <p>结果发现官方使用的是LoaderManager+cursor+cursorAdapter的方式,官方的源码看上去很复杂,但是它是一次性加载完所有数据然后返回一个Cursor,cursorAdapter利用负责展示数据,脑海中展示2个疑问,</p>    <p>1,cursor一次性加载所有数据不会延迟吗?</p>    <p>我有尝试将所有数据放在内存中,adapter加载的时候还是会延迟。</p>    <p>但是官方的通讯录在大数据量下加载非常顺滑</p>    <p>原来数据库返回Cursor的时候并没有真正去查询,打比方你普通加载99999条数据需要1000ms,返回cursor 只会耗费一秒。</p>    <p>CursorAdapter在显示的时候才会去加载数据</p>    <pre>  <code class="language-java">/** * @see android.widget.ListAdapter#getView(int, View, ViewGroup) */  public View getView(int position, View convertView, ViewGroup parent) {      if (!mDataValid) {          throw new IllegalStateException("this should only be called when the cursor is valid");      }      if (!mCursor.moveToPosition(position)) {          throw new IllegalStateException("couldn't move cursor to position " + position);      }      View v;      if (convertView == null) {          v = newView(mContext, mCursor, parent);      } else {          v = convertView;      }      bindView(v, mContext, mCursor);      return v;  }</code></pre>    <p>2,在查询所有数据的同时,加载了所有字母索引,例如我的</p>    <pre>  <code class="language-java">select sortLetters,COUNT(*) as count from member GROUP BY sortLetters order by upper(sortLetters) asc</code></pre>    <p>官方的有点复杂,这个索引还加了缓存</p>    <p>3,保存字母和字母所占数量</p>    <pre>  <code class="language-java">labels = new String[numLabels];counts = new int[numLabels];  for (int i = 0; i < numLabels; i++) {      cursor.moveToNext();      labels[i] = cursor.getString(0);       counts[i] = cursor.getInt(1);  }</code></pre>    <p>4,根据字母和数量得到字母position</p>    <pre>  <code class="language-java">mSections = sections;  mPositions = new int[counts.length];  int size = counts.length;  int position = 0;  for (int i = 0; i < size; i++) {      if (TextUtils.isEmpty(mSections[i])) {          mSections[i] = "#";      }      mPositions[i] = position;     position += counts[i];}mCount = position;</code></pre>    <p>4,根据sectionIndex 获取postion</p>    <pre>  <code class="language-java">public int getPositionForSection(int sectionIndex) {      if (mSections == null || mPositions == null) {          return -1;      }      if (sectionIndex < 0 || sectionIndex >= mSections.length) {          return -1;      }      return mPositions[sectionIndex];  }</code></pre>    <p>5,根据字母获取Position</p>    <pre>  <code class="language-java">public int getPositionForTitle(String title){      for (int i = 0; i < mSections.length; i++) {           if(mSections[i].equals(title)){               int position = getPositionForSection(i);               MLog.e("test","getPositionForTitle" + position);              return position;           }      }     return -1;  }</code></pre>    <p>根据postion获取字母所在Section</p>    <pre>  <code class="language-java">@Override  public int getSectionForPosition(int position) {      if (mSections == null || mPositions == null) {          return -1;      }      if (position < 0 || position >= mCount) {          return -1;     }      int index = Arrays.binarySearch(mPositions, position);      return index;  }</code></pre>    <p>修改过后,进入页面就能立即看到数据,以前即使在内存中直接加载,也会有几秒的空白时间</p>    <p> </p>    <p> </p>    <p>来自:http://www.jianshu.com/p/21bbd1c24b30</p>    <p> </p>    
 本文由用户 eastxiang 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1480387112825.html
数据库 安卓开发 Android开发 移动开发