How to realize a persistent queue on Android

我与影子孤独终老i 提交于 2019-12-04 05:07:25

I recommend using a table inside Sqlite. I would create my own SqliteQueue that implements the Queue interface. Offer would add entry to the table. Poll would return an entry and remove it from the table. For more complicated objects you could use GSON to convert the object to a JSON string or convert the JSON string to an object.

May squareup is a good choice.

QueueFile is a lightning-fast, transactional, file-based FIFO. Addition and removal from an instance is an O(1) operation and is atomic. Writes are synchronous; data will be written to disk before an operation returns. The underlying file is structured to survive process and even system crashes and if an I/O exception is thrown during a mutating change, the change is aborted.

This is good library from Path:

https://github.com/path/android-priority-jobqueue

"A Job Queue specifically written for Android to easily schedule jobs (tasks) that run in the background, improving UX and application stability."

And provide many features like priority, network check, callbacks etc.

I'm pretty there are no such data structures in Java API, but there should be no problem in implementing your own kind of Queue. Take a look at this document, which contains the description of Data Storage mechanisms in Android. I would suggest using SharedPreferences, but you may choose what fits your application most. You can extend one of the Java's collections which implements the Queue interface and add your persistence mechanisms using the Android's storage approaches. Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!