【问题标题】:Should I use beginTransaction() and endTransaction() both in insert() and bulkInsert()?我应该在 insert() 和 bulkInsert() 中都使用 beginTransaction() 和 endTransaction() 吗?
【发布时间】:2016-08-09 18:17:51
【问题描述】:

我正在开发一个使用 Content Provider 的 Android 应用程序。我重写了 insert()bulkInsert() 方法。

我将在不同的场景中使用 insert()bulkInsert()。 我知道 bulkInsert() 在内部使用 insert()

我需要事务是原子的。我应该在 both insertbulkInsert 中使用 beginTransaction()endTransaction() ?

【问题讨论】:

  • 修复拼写错误和语法。改进格式。
  • @Prune : 请告诉我在哪里修复拼写、语法和格式
  • 啊!那是沟通问题。我的评论是我编辑您的问题时记录的评论。你不需要再修复任何东西。 Stack Overflow 编辑软件询问“这个人对我的帖子做了什么”;它不是直接针对您的。以后我会努力记住这一点的。

标签: android performance android-sqlite android-contentprovider bulkinsert


【解决方案1】:

当您插入批量数据时,使用非常重要

BeginTransaction();
SetTransactionSuccessful();
EndTransaction();

因为如果您不这样做,SQLite 本身将使用隐式事务包装每个查询。通过使用 BeginTransaction() 和 EndTransaction() 可以提高性能。详情请阅读https://medium.com/@JasonWyatt/squeezing-performance-from-sqlite-insertions-971aff98eef2

当您只使用一条记录时,您可以忽略 BeginTransaction() 和 EndTransaction()。

类似的帖子 beginTransaction(), endTransaction(), and setTransactionSuccessful(). What exactly do they do?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 2021-06-23
    • 2012-01-15
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多