android offline handling innovationm blog

Android Offline Handling

Android offline handling basically represents the local storage data handling when there is no internet connection to get the basic required information. If not handled, the screen will be out of data and result in a blank screen which ultimately affects the UI experience.

There are two ways to handle local data :

  1. NoSql 
  2. SQL

NoSql

Storing data in NoSql format or JSON format is one of the approaches to handle data offline. PaperDB and better use of SharedPreferences are the two ways to store data in JSON format. Since SharedPreferences only stores the primitive data types, we can convert the class object into JSON string and store them.

SharedPreference

val sharedPreferences = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putString("key", "value")
editor.putInt("key", 0)
editor.putBoolean("key", true)
editor.putFloat("key", 0.0f)

# Storing class object into shared preferences

 

val user = User(
   name = etName.text.toString(),
   address = etAddress.text.toString(),
   phoneNo = etPhoneNumber.text.toString()
)
val userJson = Gson().toJson(user, User :: class.java)
editor.putString("user", userJson  ")
editor.apply()

# Fetch Data from SharedPreference

 

sharedPreferences.getBoolean("key", false)
sharedPreferences.getFloat("key", 0.0f)
sharedPreferences.getString("key", null)
sharedPreferences.getInt("key", 0)
val userJson = sharedPreferences.getString("user", null)
val user = Gson().fromJson<User>(userJson , User::class.java)


 

PaperDB

Paper’s aim is to provide a simple yet fast object storage option for Android. It allows us to use Java/Kotlin classes as is: without annotations, factory methods, mandatory class extensions etc. Moreover adding or removing fields to data classes is no longer a pain – all data structure changes are handled automatically.

 

Installing

  1. add dependency in build.gradle(app) file implementation
    'io.paperdb:paperdb:2.7.1'
  2. In Application class, add Paper.init(context) inside onCreate method

Operations

Paper.book()

   .write(“user”, user) // store and update data havingkey user’

Paper.book().delete(“user”) // delete data having key user

Paper.book().contains(“user”) // checks whether user key is available in database

val user = Paper.book()

   .read<User>(“user”) // fetch data having key user

SQL

The other approach to handle offline data to store data in table structured row and column. Data is manipulated with the help of queries.

SQLite

SQLite is an open source SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation. SQLite supports all the relational database features.

For more learnings go to https://www.tutorialspoint.com/android/android_sqlite_database.htm

Room Database

It is a layer on the top of SQLite. Room Database takes care of complicated queries and operations. It used annotations for creating tables, defining characteristics of the column, operations on the table like insert, delete, create and update. To use Room, create an abstract class extending RoomDatabase.

Installing

Add dependencies 

implementation "android.arch.persistence.room:runtime:1.1.1"

annotationProcessor "android.arch.persistence.room:compiler:1.1.1"

For more learning goto

https://medium.com/mindorks/using-room-database-android-jetpack-675a89a0e942

Leave a Reply