简体   繁体   English

Android SDCard I / O管理挂载和卸载

[英]Android SDCard I/O Managing Mount and Unmount

I know there are various version of this question asked, but I was having trouble getting a clear answer on what the best approach is for my problem. 我知道这个问题有不同的版本,但是我很难找到最佳解决方案。

What I am doing is I am creating a SQLite database on the SD card. 我正在做的是在SD卡上创建一个SQLite数据库。 I want to be able to query from it and write to it. 我希望能够从中查询并写入它。

The question I have is what is the best way to manage when the SD card is unmounted. 我的问题是卸载SD卡时最好的管理方法是什么。 I am totally fine with my application closing like the stock MP3 player does. 我的应用程序关闭就像库存的MP3播放器一样,完全可以。 However, I want to make sure any write action to the db do not get partially done. 但是,我想确保对数据库的任何写操作都不会部分完成。

One thought I had is the use beginTransaction, mark it as successful, and then call end transaction. 我曾经想到的是使用beginTransaction,将其标记为成功,然后调用结束事务。 The question I have is what happens if end of transaction does not get called. 我的问题是,如果未调用交易结束会发生什么情况。 Can that potentially lead to data corruption? 这可能导致数据损坏吗? Also, I need a little help understanding what to listen to or hook into to get notification of the sd card being unmounted. 另外,我需要一点帮助来了解要收听或收听的内容,以获取有关卸载SD卡的通知。

Thanks 谢谢

That is the great thing about transactions in databases - you almost never need to worry: 那是数据库中事务的妙处-几乎不用担心:

"All changes within a single transaction in SQLite either occur completely or not at all, even if the act of writing the change out to the disk is interrupted by “ SQLite中单个事务中的所有更改要么完全发生,要么根本不发生,即使将更改写出到磁盘的行为被中断

a program crash, an operating system crash, or a power failure." 程序崩溃,操作系统崩溃或电源故障。”

Taken from http://www.sqlite.org/transactional.html 取自http://www.sqlite.org/transactional.html

The disk being removed on which the database resides should (in the worst case) behave like a power failure while writing the data to the disk. 将数据写入磁盘时,在最坏的情况下,要删除数据库所在的磁盘的行为应类似于电源故障。 The database will discard that data on next startup. 数据库将在下次启动时丢弃该数据。

Thus, as soon as your transaction is committed using commit or end transaction and the method call executing your statement returns all data has been stored. 因此,一旦使用commit或end transaction提交了事务,并且执行语句的方法调用将返回所有已存储的数据。 Otherwise NO data from your transaction will have been stored - both cases leave you in a consistent state. 否则,将不会存储您的交易中的任何数据-两种情况都使您处于一致状态。

Beware of the only catch: You will need to make sure that all statements you need to execute together (ie atomically) must be within one transaction! 当心唯一的陷阱:您需要确保需要一起执行(即原子执行)的所有语句都必须在一个事务中!

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

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