简体   繁体   English

是否可以将Android的内置SQLite存储用于并发数据存储?

[英]Is it OK to use Android's built-in SQLite storage for concurrent data storage?

I'm writing an Android application, which will consist of 我正在编写一个Android应用程序,其中包括

  1. background service 1, which reads sensor values in regular intervals (eg every five minutes the program reads sensor values for 30 seconds, during the measurement period values are read twice per second), 后台服务1,它定期读取传感器值(例如,每五分钟,程序将在30秒内读取一次传感器值,在测量期间每秒读取两次值),
  2. background service 2, which analyzes the sensor values and calculates some metrics from them, 后台服务2,它分析传感器值并从中计算出一些指标,
  3. background service 3, which runs once in 3-7 days and removes old data from the database (both sensor values and metrics) and 后台服务3,该服务每3-7天运行一次,并从数据库中删除旧数据(传感器值和指标),并且
  4. a GUI activity, which shows some of the sensor values and/or metrics, if the user opens the application and presses some button. 如果用户打开应用程序并按一些按钮,则GUI活动将显示一些传感器值和/或度量。

There will be 2 tables - SensorData and Metrics . 将有2个表SensorDataMetrics Background service 1 will write to SensorData . 后台服务1将写入SensorData Background service 2 will read from SensorData and write to Metrics . 后台服务2将从SensorData读取并写入Metrics Background service 3 will remove rows from both tables. 后台服务3将从两个表中删除行。 The GUI activity will read from both tables. GUI活动将从这两个表中读取。

I was thinking about how to implement a threadsafe storage for the sensor values and metrics. 我在考虑如何为传感器值和指标实现线程安全存储。

An obvious solution is to use the built-in SQLite database. 一个明显的解决方案是使用内置的SQLite数据库。

I have following questions: 我有以下问题:

  1. Is it safe to use in this setting? 在此设置中使用安全吗?
  2. Will Android's SQLite handle writing of two numeric values ( long and double ) twice per second? Android的SQLite是否会每秒两次写入两个数字值( longdouble )?
  3. Does it support concurrent access (what happens, when the GUI activity issues a query on SensorData and background service 1 tries to write into the same table simultaneously) ? 它是否支持并发访问(当GUI活动在SensorData上发出查询并且后台服务1尝试同时写入同一表时会发生什么情况)?
  1. by using the SQLiteOpenHelper as a singleton, you are thread-safe. 通过将SQLiteOpenHelper作为单例使用,您是线程安全的。 SQLiteOpenHelper handles the locking of the sqlite database (which is the sqlite mechanism to handle concurrent reads/writes). SQLiteOpenHelper处理sqlite数据库的锁定(这是用于处理并发读取/写入的sqlite机制)。
  2. This is something to test. 这是要测试的东西。 but, generally speaking, do you need to write to the database that often?. 但是,总的来说,您是否需要经常写入数据库? you can cache the data in memory and flush them to the database at a slower rate. 您可以将数据缓存在内存中,然后以较低的速率将其刷新到数据库。
  3. the short answer is 'yes it supports concurrent access'. 简短的回答是“是的,它支持并发访问”。 for details, review this sqlite document , android sqlite is just a wrapper on this. 有关详细信息,请查看此sqlite文档 ,android sqlite只是对此的包装。 you can configure it to behave the way you find suitable after reading this document. 您可以在阅读本文档后对其进行配置,使其表现出您认为合适的方式。

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

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