3. Bina Nusantara Kemanggisan
SharedPreferences keeps data in a single instance to be accessed by
a class that all clients share. Shared Preferences are usually used to keep
user data and to keep users logged in to an app.
Shared Preferences
4. Bina Nusantara Kemanggisan
SharedPreferences keeps data in a class whereas Room keeps its’ data
on a local database. Room is and abstraction layer for SQLite in Android,
SQLite is usually used to keep temporary data that might be used later in the
apps lifecycle.
Room
5. Bina Nusantara Kemanggisan
Room Architecture Illustration
Image source: https://www.pluralsight.com/guides/making-a-notes-app-using-room-database
6. Bina Nusantara Kemanggisan
When using Room as an abstraction layer, it directly connects the SQLite data to the view
model using entity. With SQLite there needs to be a helper class to handle the read and
write of data to the view model, thus using Room can drastically improve performance.
Room vs SQLite
7. Bina Nusantara Kemanggisan
DAO is the interface used to make actions to interact with the Room database.
DAO actions include but are not limited to Insert, Query (Select), Delete and
Update.
Data Access Object (DAO)
11. Bina Nusantara Kemanggisan
SharedPreferences.Editor
Accepts the edit method of a SharedPreferences class, code example:
Code Highlight
1. private SharedPreferences sharedPreferences;
2. sharedPreferences =
context.getSharedPreferences(context.getResources().getString(R.string.l
ogin_preferences), Context.MODE_PRIVATE);
3. SharedPreferences.Editor editor = sharedPreferences.edit();
12. Bina Nusantara Kemanggisan
Room.databaseBuilder(context, class, string)
Used to initialize a class extending the RoomDatabase class.
Code Highlight
● context in this case is the application context.
● class in this case is the class being initialized.
● string in this case is the mock name of the database.