简体   繁体   English

如何防止Android SQLite将堆栈跟踪信息转储到logcat?

[英]How to prevent Android SQLite from dumping stack trace to logcat?

On Android 2.3.3 I'm doing a bulk import into one of my SQLite tables, using the API method: 在Android 2.3.3上,我正在使用API​​方法批量导入我的SQLite表之一:

SQLiteDatabase.insert(String table, String nullColumnHack, ContentValues values) SQLiteDatabase.insert(字符串表,字符串nullColumnHack,ContentValues值)

It works fine, but if there is a database error with any row (eg constraint error) a full stack trace is written to Android Logcat which quickly fills up. 它工作正常,但是如果任何行都存在数据库错误(例如约束错误),则将完整的堆栈跟踪写入Android Logcat,从而迅速填充。

How can I turn off this stack trace? 如何关闭此堆栈跟踪?

I already handle any Exception being thrown but the Logcat is being polluted from within the SQLite library code. 我已经处理了所有抛出的异常,但是Logcat从SQLite库代码中被污染了。

I am worried about performance on mobile devices. 我担心移动设备上的性能。

  • transactions for import are enabled already 导入交易已启用
  • preloaded DB is impossible because data might change 预加载的DB是不可能的,因为数据可能会更改

Constraints should be enforced and I will handle violation, but 20 line stack traces in Logcat for each violation multiplied by hundreds of violations is what I want to turn off. 应该强制执行约束,我将处理冲突,但是我想关闭Logcat中针对每个冲突的20行堆栈跟踪乘以数百个冲突。

Unfortunately I also cannot use advanced conflict handling introduced with Android Froyo because app must also run on older devices. 不幸的是,我也不能使用Android Froyo引入的高级冲突处理功能,因为该应用程序还必须在较旧的设备上运行。

Cheers. 干杯。

OK, I just found the solution as follows: 好的,我刚刚找到了以下解决方案:

I have to use a different API method (not SQLiteDatabase.insert() ): 我必须使用其他API方法(不是SQLiteDatabase.insert() ):

SQLiteDatabase.insertOrThrow(String table, String nullColumnHack, ContentValues values)

Now, the exception is thrown from the SQLite library, as expected and no stack trace is dumped. 现在,按预期从SQLite库引发了异常,并且没有转储堆栈跟踪。

Just filter out any stuff you're not interested in, based on the tag or the process ID. 只需根据标签或进程ID过滤掉您不感兴趣的任何内容。 You can do it easily in the Logcat viewer in Eclipse, or if you're using Logcat on the command line the instructions are here . 您可以在Eclipse的Logcat查看器中轻松完成此操作,或者,如果您在命令行上使用Logcat,请参见此处的说明

It's not pollution. 这不是污染。 It's the purpose of LogCat to register everything that's going on on the device / emulator. LogCat的目的是注册设备/仿真器上正在发生的一切。

If you want to filter out your LogCat, it's possible in Eclipse (you can filter by log tag, by pid, or by log level), thus providing a way to "remove" unwanted exceptions from a LogCat window. 如果要过滤出LogCat,则可以在Eclipse中进行过滤(可以按日志标签,PID或按日志级别过滤),从而提供了一种从LogCat窗口中“删除”有害异常的方法。

EDIT If the issue is performance (according to your comments), I don't think you should worry. 编辑如果问题是性能(根据您的评论),我认为您不必担心。

  • If you find bad performances, you'll gain A LOT more by enabling transactions on your batch insert. 如果发现性能不佳,则可以通过在批处理插入中启用事务来获得更多收益。
  • Also, if it's to insert "starting data" for your application, have you considered shipping the apk with a pre-loaded database? 另外,如果要为您的应用程序插入“起始数据”,您是否考虑过将apk与预加载的数据库一起运送? Instead of inserting everything at run-time, you could just copy an existing database that you prepared yourself. 除了在运行时插入所有内容外,您还可以复制自己准备的现有数据库。 No constraint problem anymore, and almost instant start for your users. 不再存在约束问题,几乎可以为您的用户立即启动。 Of course, if it's dynamic batch insert (based on user entry or external parameters defined at runtime), it's not possible. 当然,如果它是动态批处理插入(基于用户输入或在运行时定义的外部参数),则不可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM