使用游标避免 ANR

发布于 2024-11-15 16:27:43 字数 601 浏览 2 评论 0原文

我有一个活动,它在 Sqlite DB 上运行查询,获取游标,使用该游标创建 CustomCursorAdapter,并将其附加到活动中的 ListView。它看起来像这样:

SQLiteDatabase db=new StuffHelper(this).getWritableDatabase();
Cursor c1=db.query(StuffHelper.TABLE,
            new String[]{StuffHelper._ID},
            StuffHelper.SIZE+">=?",
            new String[]{"64"},
            null,
            null,
            null);
startManagingCursor(c1);

StuffAdapter a1 = new StuffAdapter(this, c1);

ListView ll1 = (ListView) findViewById(R.id.ll1);
ll1.setAdapter(a1);

当前的设置在 ANR 方面存在问题吗?使用游标时,如何告诉 android 在后台线程上运行所有 Sqlite 内容?

I have an activity that runs a query on a Sqlite DB, gets a Cursor, creates a CustomCursorAdapter with that Cursor, and attaches it to the ListView in the activity. It looks like this:

SQLiteDatabase db=new StuffHelper(this).getWritableDatabase();
Cursor c1=db.query(StuffHelper.TABLE,
            new String[]{StuffHelper._ID},
            StuffHelper.SIZE+">=?",
            new String[]{"64"},
            null,
            null,
            null);
startManagingCursor(c1);

StuffAdapter a1 = new StuffAdapter(this, c1);

ListView ll1 = (ListView) findViewById(R.id.ll1);
ll1.setAdapter(a1);

Is this current setup a problem in terms of ANR? When using cursors, how can I tell android to run all the Sqlite stuff on a background thread?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不甘平庸 2024-11-22 16:27:43

您没有提供太多有关此代码何时运行的上下文,但无论如何我都会咬住......

是的,它确实存在 ANR 的风险。它还面临着各种其他生命周期问题的风险。由于 setListAdapter() 需要在您通常在 onCreate() 中执行的各种其他操作之前调用,因此您可能希望将数据库访问卸载到单独的线程(例如一个 AsyncTask),可以根据需要调用/缓存/管理。 AsyncTask 在线程启动之前为您提供基于 UI 的回调,并在线程结束时提供基于 UI 的回调。可以创建和分配 ListAdapter,而不需要任何对 Cursor 的引用(我建议您尽快修复该问题......似乎没有充分的理由为什么您使用自定义列表适配器,您应该管理相反,您的数据库访问更好)。

管理这项任务而不是活动拆除和重建(想想改变方向……)是一个完全不同的蜡球,并且已经在 SO 上被覆盖得令人作呕。

You didn't give much context of when this code is run, but I'll bite anyway...

Yes, it does run the risk of an ANR. It also runs the risk of various other lifecycle problems. Since setListAdapter() needs to be called before various other thing that you'd normally do in onCreate() you probably want to offload the database access to a separate thread (like an AsyncTask) that can be called/cached/managed as needed. AsyncTask gives you a UI-based callback before the thread starts and a UI-based callback when the thread ends. The ListAdapter can be created and assigned without any references to a Cursor (and I'd suggest you fix that asap... there doesn't seem to be a good reason why you're using a custom list adapter, you should be managing your database access better instead).

Managing this task over activity teardown and rebuilding (think changing orientation...) is an entirely different ball of wax and has been covered ad nauseam on SO.

筱果果 2024-11-22 16:27:43

请将您的 UI 与后台任务分开。在后台写入光标部分,在前台可以显示任何 UI 或进度对话框。使用AsyncTask这个

AsyncTask类有以下方法

1. doInBackground: Code performing long running operation goes in this method.  When onClick method is executed on click of button, it calls execute method which accepts parameters and automatically calls doInBackground method with the parameters passed.
   2. onPostExecute: This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method.
   3. onPreExecute: This method is called before doInBackground method is called.
   4. onProgressUpdate: This method is invoked by calling publishProgress anytime from doInBackground call this method.

如何使用AsyncTask谢谢


迪帕克

Please separate your UI from background tasks. Write the cursor portion in background and in forground you can show any UI or progress dialog. Use AsyncTask for this

AsyncTask class has following methods

1. doInBackground: Code performing long running operation goes in this method.  When onClick method is executed on click of button, it calls execute method which accepts parameters and automatically calls doInBackground method with the parameters passed.
   2. onPostExecute: This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method.
   3. onPreExecute: This method is called before doInBackground method is called.
   4. onProgressUpdate: This method is invoked by calling publishProgress anytime from doInBackground call this method.

How to use AsyncTask

Thanks
Deepak

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文