Skip to content

Commit aeb2618

Browse files
committed
Add a section about persisting data
1 parent c7f0f80 commit aeb2618

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Lessons learned from Android developers in [Futurice](http://www.futurice.com).
2323
#### Use Robolectric for unit tests, Robotium for connected (UI) tests
2424
#### Use Genymotion as your emulator
2525
#### Always use ProGuard or DexGuard
26+
#### Use SharedPreferences for simple persistence, otherwise ContentProviders
2627

2728

2829
----------
@@ -520,6 +521,33 @@ Read more at [Proguard](http://proguard.sourceforge.net/#manual/examples.html) f
520521

521522
**DexGuard**. If you need hard-core tools for optimizing, and specially obfuscating release code, consider [DexGuard](http://www.saikoa.com/dexguard), a commercial software made by the same team that built ProGuard. It can also easily split Dex files to solve the 65k methods limitation.
522523

524+
### Data storage
525+
526+
527+
#### SharedPreferences
528+
529+
If you only need to persist simple flags and your application runs in a single process SharedPreferences is probably enough for you. It is a good default option.
530+
531+
There are two reasons why you might not want to use SharedPreferences:
532+
533+
* *Performance*: Your data is complex or there is a lot of it
534+
* *Multiple processes accessing the data*: You have widgets or remote services that run in their own processes and require synchronized data
535+
536+
537+
#### ContentProviders
538+
539+
In the case SharedPreferences is not enough for you, you should use the platform standard ContentProviders, which are fast and process safe.
540+
541+
The single problem with ContentProviders is the amount of boilerplate code that is needed to set them up, as well as low quality tutorials. It is possible, however, to generate the ContentProvider by using a library such as [Schematic](https://github.com/SimonVT/schematic), which significantly reduces the effort.
542+
543+
You still need to write some parsing code yourself to read the data objects from the Sqlite columns and vice versa. It is possible to serialize the data objects, for instance with Gson, and only persist the resulting string. In this way you lose in performance but on the other hand you do not need to declare a column for all the fields of the data class.
544+
545+
546+
#### Using an ORM
547+
548+
We generally do not recommend using an Object-Relation Mapping library unless you have unusually complex data and you have a dire need. They tend to be complex and require time to learn. If you decide to go with an ORM you should pay attention to whether or not it is _process safe_ if your application requires it, as many of the existing ORM solutions surprisingly are not.
549+
550+
523551
### Thanks to
524552

525553
Antti Lammi, Joni Karppinen, Peter Tackage, Timo Tuominen, Vera Izrailit, Vihtori Mäntylä, Mark Voit, Andre Medeiros, Paul Houghton and other Futurice developers for sharing their knowledge on Android development.

0 commit comments

Comments
 (0)