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

# Storing class object into shared preferences

 

# Fetch Data from SharedPreference

 

 

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
  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 

For more learning goto

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

Leave a Reply