【问题标题】:swapCursor() slow: "The application may be doing too much work on its main thread."swapCursor() 慢:“应用程序可能在其主线程上做了太多工作。”
【发布时间】:2014-07-07 09:57:13
【问题描述】:

我不断看到“应用程序可能在其主线程上做的工作过多。”

这是由我下面代码中的 swapCursor() 引起的吗?看起来是这样:如果我删除它,上面的警告就会消失。

我仍然不明白为什么它声称是导致问题的主线程。我已经将 swapCursor() 移动到不同的地方,例如 onLoadFinished() 和 loadInBackground() 但我得到了相同的结果。

如何调用 swapCursor() 以使该警告不出现?我使用 SimpleCursorLoader 的全部原因是避免阻塞主线程,但它仍在发生。

package com.example.sqlitetest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.content.CursorLoader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> 
{
  public static Context mContext;

  private LoaderManager.LoaderCallbacks<Cursor> mLoaderCallbacks;

  public static SQLiteDatabase      db;
  public static SimpleCursorAdapter cAdapter;
  public static ListView            lvCustomList;


  public static final class MyCursorLoader extends SimpleCursorLoader 
  {
    public MyCursorLoader( Context context )
    {
      super( context );
    }

    @Override
    public Cursor loadInBackground()
    {
      Cursor cursor = null;

      cursor = db.rawQuery( "SELECT rowid _id, Name, Rating FROM Tune ORDER BY Name", null );

      cAdapter.swapCursor( cursor );

      return cursor;
    }
  }  

  @Override
  public Loader<Cursor> onCreateLoader(int id, Bundle args) 
  {
    return new MyCursorLoader( this );
  }

  @Override
  public void onLoadFinished( Loader<Cursor> loader, Cursor cursor ) 
  {
//    cAdapter.swapCursor( cursor );
  }

  @Override
  public void onLoaderReset( Loader<Cursor> loader ) 
  {
    cAdapter.swapCursor( null );
  }

  @Override
  public void onCreate( Bundle savedInstanceState )
  {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.tune_artist_album_view );

    mContext = this;

    String path = "/sdcard/MyDb.sqlite";

    db = SQLiteDatabase.openDatabase( path, null, 0 );

    lvCustomList = (ListView) findViewById( R.id.lv_custom_list );

    String[] columns = new String[] { "Name", "Rating" };

    int[] to = new int[] { R.id.lv_tune };  

    cAdapter = new SimpleCursorAdapter( mContext, R.layout.like_hate_row, null, columns, to, 0 );

    lvCustomList.setAdapter( cAdapter );

    mLoaderCallbacks = this;

    LoaderManager lm = getSupportLoaderManager();

    lm.initLoader( 0, null, mLoaderCallbacks );
  }
}

这里是 SimpleCursorLoader:

package com.example.sqlitetest;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.content.AsyncTaskLoader;

/**
 * Used to write apps that run on platforms prior to Android 3.0. When running
 * on Android 3.0 or above, this implementation is still used; it does not try
 * to switch to the framework's implementation. See the framework SDK
 * documentation for a class overview.
 * 
 * This was based on the CursorLoader class
 */
public abstract class SimpleCursorLoader extends AsyncTaskLoader<Cursor>
{
  private Cursor mCursor;

  public SimpleCursorLoader( Context context )
  {
    super( context );
  }

  /* Runs on a worker thread */
  @Override
  public abstract Cursor loadInBackground();

  /* Runs on the UI thread */
  @Override
  public void deliverResult( Cursor cursor )
  {
    if( isReset() )
    {
      // An async query came in while the loader is stopped
      if( cursor != null )
      {
        cursor.close();
      }
      return;
    }

    Cursor oldCursor = mCursor;
    mCursor = cursor;

    if( isStarted() )
    {
      super.deliverResult( cursor );
    }

    if( oldCursor != null && oldCursor != cursor && !oldCursor.isClosed() )
    {
      oldCursor.close();
    }
  }

  /**
   * Starts an asynchronous load of the contacts list data. When the result is
   * ready the callbacks will be called on the UI thread. If a previous load has
   * been completed and is still valid the result may be passed to the callbacks
   * immediately.
   * <p/>
   * Must be called from the UI thread
   */
  @Override
  protected void onStartLoading()
  {
    if( mCursor != null )
    {
      deliverResult( mCursor );
    }
    if( takeContentChanged() || mCursor == null )
    {
      forceLoad();
    }
  }

  /**
   * Must be called from the UI thread
   */
  @Override
  protected void onStopLoading()
  {
    // Attempt to cancel the current load task if possible.
    cancelLoad();
  }

  @Override
  public void onCanceled( Cursor cursor )
  {
    if( cursor != null && !cursor.isClosed() )
    {
      cursor.close();
    }
  }

  @Override
  protected void onReset()
  {
    super.onReset();

    // Ensure the loader is stopped
    onStopLoading();

    if( mCursor != null && !mCursor.isClosed() )
    {
      mCursor.close();
    }
    mCursor = null;
  }
}

【问题讨论】:

  • 使用 Traceview 找出您的时间都花在了哪里,而不是猜测。
  • @CommonsWare:谢谢,我会试一试,看看它显示了什么。午餐时间在我的手机上试了一下,没有出现任何警告。问题可能只出现在我的平板电脑上。
  • @CommonsWare:好的,所以 TraceView 告诉我我的猜测是正确的——在主线程上执行 nativeExecuteForCursorWindow() 花费了 1.5 秒。为什么在主线程上?我显然没有实现在后台进行数据库检索的目标。

标签: android android-listview simplecursoradapter android-cursorloader android-loadermanager


【解决方案1】:

query()rawQuery() 之类的方法不会像您认为的那样做。值得注意的是,它们实际上并不查询数据库。他们只是配置他们返回的SQLiteCursor。当您尝试使用 Cursor 时,它会执行查询。

因此,在您的loadInBackground() 方法中,在rawQuery()swapCursor() 之间,在您从rawQuery() 返回的Cursor 上调用getCount()。这足以迫使Cursor 真正完成查询工作,因此该工作在后台线程上完成。

【讨论】:

  • 啊哈!感谢那。我在一些示例中看到了 getCount() 调用。返回值被忽略了,我认为它只是一些遗留的意外代码 - 这解释了它的存在!我会试一试的。如果这有帮助,我可以接受你的回答。
  • @SparkyNZ:唉,没有yoYouLazyObjectDoTheQueryAlready() 方法,否则,我们会在这个用例中使用它。 :-)
  • 是的,成功了。我没有在主线程上看到 1.5 秒的块,而是看到 ModernAsyncTask 做了这么多的工作......并且没有关于丢失帧的警告。我已接受您的回答 - 非常感谢您的回答……以及 TraceView 的有趣旅程。
  • 现在是 2016 年 3 月,CursorLoader 仍在发生这种情况。幸好找到了
猜你喜欢
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多