DA4. Data Persistence in Android using SQLite¶
Statement¶
- Is it possible to handle data persistence in android using SQLite database with all four CRUD operations?
- If yes, explain how it is done.
- If no, justify your answer.
Solution¶
The user’s Android device is a client device, that’s considered stateless by default, meaning it is difficult to keep state between sessions, and the data is lost when the app is closed. However, there are many ways to handle data persistence in Android, like:
- Having a backend server that stores data (in some database), while the pushes/pulls data to/from the server through out the network (e.g. using REST API).
- Having a local database on the client’s device (Android end), that stores data locally, and when the app is opened, it loads the state from the local database, and updates it regularly throughout the session.
- Many other ways, like storing data in raw text file on the client end, or using a cloud service like Firebase.
The first option is popular among web apps as browsers usually isolated from the client device and do not have complex permissions. However, the beauty of a native application that is has access to the core OS (or platform) APIs, which makes the second option viable. The third option, on the other hand, is complex and usually requires functionality that is beyond the purpose of the app itself.
The app can create an instance of a SQLite database during installation, and ensures that this database does exist when the app is opened. The SQLite is known to be lightweight, which gave it the possibility to be shipped to the client as opposed to other database management systems which are more complex and heavy. However, recently more options are available for data persistence in Android, like Realm (MongoDB, n.d.).
The client side’s SQLite is able to perform all CRUD operations, and it can do anything that a database can do as long as the client capabilities allow it to do so. The data can not be saved though, as usually uninstalling the app will remove the database, and the data will be lost; along with the fact that the database is not accessible to other apps, and the data is not shared between apps; and the limited storage space on the client device; all of these are considered disadvantages of using SQLite for data persistence in Android (Chaitanyamunje, 2021).
The prefect use case includes persisting data temporarily, while connection is lost and all data is synced to cloud or remote server as soon as the internet connection is available. This is a common use case for many apps, like social media apps, where the user can use the app offline, and the data is synced when the user is online. These techniques also help in implementing some good caching solutions on the client side, which reduces the load on the server side.
Thus, we conclude that it is possible to handle data persistence in Android using SQLite database with all four CRUD operations, but it is not the best option for all use cases. It is suitable for caching solutions and temporary data persistence, but not for long term data persistence as dedicated database management systems and application servers are required for that.
References¶
- MongoDB. (n.d). Realm https://realm.io/
- Chaitanyamunje. (2021, February 25). How to create and add data to SQLite database in Android? GeeksforGeeks. Retrieved July 3, 2022, from https://www.geeksforgeeks.org/how-to-create-and-add-data-to-sqlite-database-in-android/