SQLite 在 Android 中插入超时?
在 onCreate() 期间,我尝试将大约 20 行插入 SQLite 数据库(这些只是测试值。对于最终版本,我必须插入大约 1000 行)。
使用调试器似乎在运行 onCreate() 时会导致“HistoryRecord 的活动暂停超时”,并且从日志中看来并非所有行都被插入。
使用异步任务是在没有超时的情况下完成这项工作的唯一方法吗?
谢谢
During onCreate() I'm trying to insert about 20 rows into the SQLite database (these are just test values. For the final version I'll have to insert about 1000).
Using the debugger it appears that when running onCreate() this causes a "Activity pause timeout for HistoryRecord", and it seems from the log not all rows are inserted.
Is using an asyncronous task the only way to make this work without the timeout?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,异步任务或在单独的线程中是正确的方法,此外,您可以告诉数据库您正在启动一个事务,这可以显着加快大型插入的过程,
dh.getDb().beginTransaction();
//插入状态
dh.getDb().setTransactionSuccessful();
dh.getDb().endTransaction();
Yes asyncronous task or in a separate Thread is the right way, additionally you can tell the db that you are starting a transaction which speeds up the process significantly for large inserts,
dh.getDb().beginTransaction();
//insert statemetns
dh.getDb().setTransactionSuccessful();
dh.getDb().endTransaction();
如果它抛出 20 条记录的警告,想象一下有 1000 条记录的延迟。你真的想阻止执行这么长时间吗?
将数据库更新为异步任务或在单独的线程中是正确的方法。
If it throws that warning with 20 records, imagine the delay with a thousand. Do you really want to block execution for so long?
Updating the database as an asyncronous task or in a separate Thread is the right way.
使用异步任务不是唯一的方法,但却是最好的方法。 AsyncTask 也非常简单。您绝对不想在主线程中执行此操作。
Using an asynchronous task is not the only way, but it is the best way. AsyncTask is also pretty easy. You DEFINITELY do not want to do this in the main thread.