在 Android 中实现 DataStore 的技巧?
我正在致力于在 Android 上提供一个数据存储(不使用 SQLite),从我们的 Java 库的单应用程序桌面版本开始。理想的解决方案是:
- 在自己的进程中执行,通过某种形式的 IPC
- 持久性提供对其他应用程序的访问,因此使用它的应用程序不会在手机关闭时丢失数据
- 合理响应
使用我的客户端-服务器架构背景,我的初步尝试的方法是使用 Service
,其他应用程序可以使用 bindService
和其他调用与该服务进行通信。结果具有上述质量#1(在某种程度上也是#3),但我不清楚何时将所有这些数据写回文件系统,特别是因为事实证明 onDestroy()< /code> 方法不保证被调用,特别是在机器关闭期间。使用警报定期保存数据也不是一个好的解决方案,因为它可能会导致进程被终止时未提交的更改。
现在,我正在考虑使用 ContentProvider
,我相信这也是哲学上最好的方法。然而问题 #2 仍然存在:如何保证系统关闭时不会残留未提交的更改,同时又不会通过使用简单的技术(例如在每个操作上写入文件系统)来牺牲性能?我想知道原生 SQLite 实现是如何实现的?
请建议执行此操作的正确方法。这应该不会像我发现的那么难:)。
谢谢。
I am working on providing a data store on Android (one that does not use SQLite), starting with a single-app desktop version of our Java Library. The ideal solution will be:
- executed in its own process, providing access to other applications by some form of IPC
- persistent, and therefore the application using it will not lose data upon phone shutdown
- reasonably responsive
Using my client-server architecture background, my initial attempt was to use a Service
, with which the other applications can communicate using bindService
and other calls. The resulting had quality #1 (and #3 to some extent) above, but it was not clear to me as to when to write all this data back to the filesystem, especially since it turns out that the onDestroy()
method is not guaranteed to be called, especially during machine shutdown. Using Alarms to periodically save data is not a good solution either, since it might lead to uncommitted changes when the process is killed.
Now, I am thinking of using a ContentProvider
, which I believe is also the philosophically best way. However problem #2 still remains: how can I guarantee that uncommitted changes are not remaining when the system shuts down, while not sacrificing on performance by using naive techniques such as writing to filesystem on each operation? I wonder how the native SQLite implementation achieves it?
Please suggest the proper way to go about doing this. This should not be as hard as I am finding it to be :).
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论