Version First Edition
Platform Android 10
Language Kotlin 1.3
Editor AS 3.5

Save Data on Android!

The persistence of data has always been a fundamental part of any application. Saving data locally or remotely with the modern techniques for synchronization allows your app to be always up-to-date, reactively presenting fresh data.

This book is for intermediate Kotlin or Android developers who want to know how to persist data using the standard Android APIs, the Room architecture component or what Google Firebase can offer.

Topics Covered in This Book

  • Persistence with Android SDK: Learn how to manage files, SharedPreferences or SQLite databases using the APIs the Android platform has to offer by default.
  • Using Room: Room is one of the most important Google Architecture Component. It allows managing entities and relations using classic Object-Oriented principles. In this book, you’ll learn everything you need to store data and run queries on top of it.
  • Manage relations with Room: A database has entities and relations. With this book, you’ll learn how to design your DB and how to manage relations eagerly and lazily.
  • Managing and testing Migrations: Every application evolves in time. Here you’ll learn how to manage migrations with Room and how to test them properly.
  • Firebase Realtime Database: If you want to manage data locally and in remote, you can use the tools provided by Google through the Firebase platform. With Firebase Realtime Database you can manage and keep in sync data in a very simple and efficient way.
  • Cloud Storage: Another option provided by Google is the Cloud Storage which allows you to leverage all the power of Google infrastructure to manage your data and run expensive queries.

Before You Begin

This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more.

i

iii

Saving Data Using Android SDK

Managing persistence is one of the main features that every mobile environment should provide, and Android is no different. In this chapter, you’ll learn, through practical examples, how to use the API that Android SDK provides to persist data. You’ll learn when and how to manage persistence depending on the type and quantity of data.

Android contains most of the API of Java and so the abstractions into the java.io package which allow you to deal with Files. In this chapter, you'll learn how to create, write, update and delete data into files. Here you'll have the opportunity to manage security using encryption and permissions.

1

SharedPreferences are useful if you need to persist a small quantity of data like texts or a set of values of primitive types. In this chapter, you'll learn how to persist and recover a small quantity of data.

2

Android also provides SQLite as a small and powerful DataBase Management System. In this chapter, you'll learn how to create a DB with SQLite and how to execute queries. You'll also learn how to manage the lifecycle of a DB from the creation to the upgrade or downgrade of versions.

3

Android also provides SQLite as a small and powerful DataBase Management System. In this chapter, you'll learn how to create a DB with SQLite and how to execute queries. You'll also learn how to manage the lifecycle of a DB from the creation to the upgrade or downgrade of versions.

4

Using Room

At Google I/O 2018, Google presented a set of new components for Android development with the name of Architecture Components. The goal was to provide a set of solutions for the most common problem in the development of Android applications. The solution for persistence is Room which is the topic of this section. You’ll learn how to use this library in the most common scenarios.

In this first chapter of the section, you'll learn what Room is and how it works. You'll see what the main components are, how to configure the library and start creating a Database.

5

You can define a database as a set of entities and relations. In this chapter, you'll learn how to create a DB with Room starting from the entities. You'll create, step by step, a sample application using the annotations provided by the framework.

6

As said in the previous chapter, entities and relations are the most important concepts in a relational DB. In this chapter, you'll learn how to define relations and how to read related data efficiently.

7

Data Access Object is the pattern Room has implemented to execute queries on top of a specific set of entities. In this chapter, you'll learn how to define relations between entities and how to optimize queries between them.

8

Room is not the only Architecture Component. Google also announced components like Lifecycle, LiveData, and DataModel. In this chapter, you'll learn how to use Room with the other architecture components.

9

Every Database has a lifecycle and needs to be updated. Applications continuously change and it's important to update the schema of a DB without losing any data or relations. In this chapter, you'll learn how to manage data migrations with Room in a simple and declarative way.

10

Using Firebase

Firebase is a mature suite of products that allow you to implement Android applications that persist information in a safe, secure and reliable way. In this section, you’ll learn the fundamentals and more advanced concepts of Firebase, including Realtime Database, as well as usage and performance.

In this first chapter, you'll learn what Firebase is and why Google decided to provide a product like it. You'll learn about its history and you'll start creating your first project using the Firebase Console. You'll also have the first introduction to all the products of the Firebase suite.

11

In this chapter, you'll learn how to configure a project with the Realtime Database features provided by Firebase. This is a very important chapter because it contains configuration details that are very important also for the following chapters. You'll learn how to download and install into your project the JSON configuration file and how to manage authentication.

12

CRUD means Create Retrieve Update and Delete and it's a way to summarise the main operations you can do on a DB. In this chapter, you'll learn how to execute read and write operations into a Realtime Database from an Android application.

13

Mobile applications run on a device with limited resources. In particular, a phone or a tablet is not always connected to the network and some feature has to be implemented when you need to write and read data from a remote database. In this chapter, you'll learn how to manage data when the device is offline.

14

Everything has a cost. In this chapter, you'll learn about two important aspects of every application and in particular of every mobile application: cost and performance.

15

You've already seen how to use a Firebase Realtime Database. In this chapter, you'll learn how to use another similar product which allows you to store and synchronize data from a mobile device into a NoSQL database: Cloud Firestore.

16

In this chapter, you'll learn how to set up an application into Cloud Firestore. Implementing the WhatsUp application, you'll learn how to create a DB using the Firebase console and how to set up the main configurations.

17

In this chapter, you'll continue working on the WhatsUp application and you'll focus on implementing reading logic. In the process, you'll learn how to read data from the Firestore, how to listen for updates in real-time, and how queries work.

18

In this chapter, you'll learn what are security rules in Cloud Firestore and how to add them to your database to make your data safe.

19

In this last chapter, you'll learn how to store media files using another Firebase feature called Cloud Storage. You'll learn how to store an image to the cloud and how to get a URL to the image to display it in your app.

20

Meet the team

Contributors

Who is this book for

This book is for intermediate Kotlin or Android developers who want to know how to persist data using the standard Android APIs, the Room architecture component or what Google Firebase can offer.

Concepts covered in this book

  • Persistence with Android SDK
  • Using Room
  • Manage relations with Room
  • Managing and testing Migrations
  • Firebase Realtime Database
  • Cloud Storage

Version history

First Edition · Android 10, Kotlin 1.3, AS 3.5

v1.0 · Sep 18 2019 · Dean Djermanović, Aldo Olivares & Jenn Bailey
Pro

Saving Data on Android

By Dean Djermanović, Aldo Olivares and Jenn Bailey

A book that will teach you to persist data on Android — saving locally or remotely — along with the modern techniques for synchronization, allowing your app to be reactive and always up-to-date.

Read for Free with an Ultimate Pro Subscription* * Includes this and all other books in our online library See all benefits
Buy Individually $59.99* *Includes access to all of our online reading features.
See buying options
Hide buying options

All videos. All books.
One low price.

A raywenderlich.com subscription is the best way to learn and master mobile development — plans start at just $19.99/month!

Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.