persistent storage mechanisms - the college of …cs5957/media/murdock 2... · persistent storage...
TRANSCRIPT
![Page 1: Persistent Storage Mechanisms - The College of …cs5957/media/Murdock 2... · Persistent Storage Mechanisms! Subtitles are for chumps. ... Need a fallback if connection is ... Shared!Preferences!](https://reader034.vdocuments.us/reader034/viewer/2022051718/5a710de67f8b9aac538c7ebe/html5/thumbnails/1.jpg)
Persistent Storage Mechanisms Subtitles are for chumps
![Page 2: Persistent Storage Mechanisms - The College of …cs5957/media/Murdock 2... · Persistent Storage Mechanisms! Subtitles are for chumps. ... Need a fallback if connection is ... Shared!Preferences!](https://reader034.vdocuments.us/reader034/viewer/2022051718/5a710de67f8b9aac538c7ebe/html5/thumbnails/2.jpg)
So, you’ve got an idea
![Page 3: Persistent Storage Mechanisms - The College of …cs5957/media/Murdock 2... · Persistent Storage Mechanisms! Subtitles are for chumps. ... Need a fallback if connection is ... Shared!Preferences!](https://reader034.vdocuments.us/reader034/viewer/2022051718/5a710de67f8b9aac538c7ebe/html5/thumbnails/3.jpg)
Persistent Storage Options § Shared Preferences § Internal Storage (private documents) § External Storage (public documents) § SQLite Database § Network Connection
![Page 4: Persistent Storage Mechanisms - The College of …cs5957/media/Murdock 2... · Persistent Storage Mechanisms! Subtitles are for chumps. ... Need a fallback if connection is ... Shared!Preferences!](https://reader034.vdocuments.us/reader034/viewer/2022051718/5a710de67f8b9aac538c7ebe/html5/thumbnails/4.jpg)
Shared Preferences § Private dictionary § Key value pairs § All keys are Strings § Stores primitive types (int, double, String)
![Page 5: Persistent Storage Mechanisms - The College of …cs5957/media/Murdock 2... · Persistent Storage Mechanisms! Subtitles are for chumps. ... Need a fallback if connection is ... Shared!Preferences!](https://reader034.vdocuments.us/reader034/viewer/2022051718/5a710de67f8b9aac538c7ebe/html5/thumbnails/5.jpg)
Shared Preferences SharedPreferences prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
String value = prefs.getString(“key”); SharedPreferences.Editor editor = prefs.edit(); editor.putString(“key”, “value”); editor.commit();
![Page 6: Persistent Storage Mechanisms - The College of …cs5957/media/Murdock 2... · Persistent Storage Mechanisms! Subtitles are for chumps. ... Need a fallback if connection is ... Shared!Preferences!](https://reader034.vdocuments.us/reader034/viewer/2022051718/5a710de67f8b9aac538c7ebe/html5/thumbnails/6.jpg)
Internal Storage § Private documents folder, similar to iOS. § Deleted when app is deleted. § Files are accessed through API calls. File toWrite = openFileOutput(…); File toRead = openFileInput(…);
![Page 7: Persistent Storage Mechanisms - The College of …cs5957/media/Murdock 2... · Persistent Storage Mechanisms! Subtitles are for chumps. ... Need a fallback if connection is ... Shared!Preferences!](https://reader034.vdocuments.us/reader034/viewer/2022051718/5a710de67f8b9aac538c7ebe/html5/thumbnails/7.jpg)
Internal Cache § Private folder meant for cache files. § Can be emptied by the system or user if
running out of storage. § Not “cleaned up” automatically File cacheDir = context.getCacheDir();
![Page 8: Persistent Storage Mechanisms - The College of …cs5957/media/Murdock 2... · Persistent Storage Mechanisms! Subtitles are for chumps. ... Need a fallback if connection is ... Shared!Preferences!](https://reader034.vdocuments.us/reader034/viewer/2022051718/5a710de67f8b9aac538c7ebe/html5/thumbnails/8.jpg)
External Storage § Public file storage. § Different on every device. Some are built
in and some are removable. Some have both.
§ Not always mounted.
getExternalFilesDir(); // API 8+ getExternalStorageDirectory(); // API 7-‐
![Page 9: Persistent Storage Mechanisms - The College of …cs5957/media/Murdock 2... · Persistent Storage Mechanisms! Subtitles are for chumps. ... Need a fallback if connection is ... Shared!Preferences!](https://reader034.vdocuments.us/reader034/viewer/2022051718/5a710de67f8b9aac538c7ebe/html5/thumbnails/9.jpg)
File § Represents files and directories. § Can be read/written using FileInputStream
and FileOutputStream. String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes()); fos.close();
![Page 10: Persistent Storage Mechanisms - The College of …cs5957/media/Murdock 2... · Persistent Storage Mechanisms! Subtitles are for chumps. ... Need a fallback if connection is ... Shared!Preferences!](https://reader034.vdocuments.us/reader034/viewer/2022051718/5a710de67f8b9aac538c7ebe/html5/thumbnails/10.jpg)
SQLite Database § C based relational database. Popular in
embedded systems. § Supported by many languages/platforms. § Single file, extremely portable. § Runs in application process (is not a
separate service). § Implements most of the SQL standard.
![Page 11: Persistent Storage Mechanisms - The College of …cs5957/media/Murdock 2... · Persistent Storage Mechanisms! Subtitles are for chumps. ... Need a fallback if connection is ... Shared!Preferences!](https://reader034.vdocuments.us/reader034/viewer/2022051718/5a710de67f8b9aac538c7ebe/html5/thumbnails/11.jpg)
SQLite Database § Stored in internal storage. Accessed by
subclassing SQLiteHelper class. § Queries return a Cursor object (collection
of rows). § Must query using SQL. No ORM (object
relational mapping). § Third party ORMs do exist (greenDAO or
ORMLite)
![Page 12: Persistent Storage Mechanisms - The College of …cs5957/media/Murdock 2... · Persistent Storage Mechanisms! Subtitles are for chumps. ... Need a fallback if connection is ... Shared!Preferences!](https://reader034.vdocuments.us/reader034/viewer/2022051718/5a710de67f8b9aac538c7ebe/html5/thumbnails/12.jpg)
Network Connection § Unlimited options. § Requires serialization like json or xml. § Often a database wrapped in a web
service. § Introduces connectivity and syncing
issues. § Need a fallback if connection is lost.
![Page 13: Persistent Storage Mechanisms - The College of …cs5957/media/Murdock 2... · Persistent Storage Mechanisms! Subtitles are for chumps. ... Need a fallback if connection is ... Shared!Preferences!](https://reader034.vdocuments.us/reader034/viewer/2022051718/5a710de67f8b9aac538c7ebe/html5/thumbnails/13.jpg)
Which one do I use?
![Page 14: Persistent Storage Mechanisms - The College of …cs5957/media/Murdock 2... · Persistent Storage Mechanisms! Subtitles are for chumps. ... Need a fallback if connection is ... Shared!Preferences!](https://reader034.vdocuments.us/reader034/viewer/2022051718/5a710de67f8b9aac538c7ebe/html5/thumbnails/14.jpg)
Shared Preferences • Simple, fast, secure. • Dic6onary paradigm. • Can only store primi6ves. Internal Storage • Private files. Secure. • Deleted when app is deleted. External Storage • Public files. Allows sharing, or for
user to bring their own files. • Not deleted by system ever,
including factory reset (in most cases).
• Can be altered by other programs.
Internal/External Cache • System allowed to delete if needed. • Otherwise same as internal/external. SQLite Database • Supports more complex data
models. • Allows for dynamic querying,
filtering, sor6ng. • Not as sophis6cated as other
databases. Network Connec6on • Can be anything you need. • Allows integra6on with other
plaLorms. • Affected by connec6vity issues.