The following is the first few sections of a chapter from Exploring Android, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Setting Up a Repository

So, now we have a ToDoModel. Wonderful!

But, this raises the question: where do ToDoModel instances come from?

In the long term, we will be storing our to-do items in a database. For the moment, to get our UI going, we can just cache them in memory. We could, if desired, have a server somewhere that is the “system of record” for our to-do items, with the local database serving as a persistent cache.

Ideally, our UI code does not have to care about any of that. And, ideally, our code that does have to deal with all of the storage work does not care about how our UI is written.

One pattern for enforcing that sort of separation is to use a repository. The repository handles all of the data storage and retrieval work. Exactly how it does that is up to the repository itself. It offers a fairly generic API that does not “get into the weeds” of the particular storage techniques that it uses. The UI layer works with the repository to get data, create new data, update or delete existing data, and so on, and the repository does the actual work.

So, in this tutorial, we will set up a simple repository. Right now, that will just be an in-memory cache, but in later tutorials we will move that data to a database.

This is a continuation of the work we did in the previous tutorial. The book’s GitHub repository contains the results of the previous tutorial as well as the results of completing the work in this tutorial.

NOTE: Starting with this tutorial, we will stop reminding you about adding import statements for newly-referenced classes. By now, you should be used to the pattern of adding import statements. If you see a class name show up in red, and Android Studio says that it does not know about that class, most likely you need to add an import statement for it.

Read Me!

This tutorial assumes that you have learned about the repository pattern from somewhere. That could be:

Step #1: Adding the Class

The preview of this section was lost due to a rupture in the space-time continuum.

Step #2: Offering a Singleton

The preview of this section is in the process of being translated from its native Klingon.

Step #3: Creating Some Fake Data

The preview of this section is en route to Mars.

Step #4: Publishing Our Data

The preview of this section is [REDACTED].

What We Changed

The preview of this section was the victim of a MITM ('Martian in the middle') attack.